In golang, how to determine the final URL after a series of redirects?

前端 未结 1 1800
甜味超标
甜味超标 2020-12-30 22:01

So, I\'m using the net/http package. I\'m GETting a URL that I know for certain is redirecting. It may even redirect a couple of times before landing on the final URL. Re

相关标签:
1条回答
  • 2020-12-30 22:21
    package main
    
    import (
        "fmt"
        "log"
        "net/http"
    )
    
    func main() {
        resp, err := http.Get("http://stackoverflow.com/q/16784419/727643")
        if err != nil {
            log.Fatalf("http.Get => %v", err.Error())
        }
    
        // Your magic function. The Request in the Response is the last URL the
        // client tried to access.
        finalURL := resp.Request.URL.String()
    
        fmt.Printf("The URL you ended up at is: %v\n", finalURL)
    }
    

    Output:

    The URL you ended up at is: http://stackoverflow.com/questions/16784419/in-golang-how-to-determine-the-final-url-after-a-series-of-redirects
    
    0 讨论(0)
提交回复
热议问题