Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 在golang,经过一系列重定向后,如何确定最终URL?_Http_Go - Fatal编程技术网

Http 在golang,经过一系列重定向后,如何确定最终URL?

Http 在golang,经过一系列重定向后,如何确定最终URL?,http,go,Http,Go,所以,我使用的是net/http包。我得到一个网址,我知道肯定是重定向。它甚至可能在登录到最终URL之前重定向几次。重定向在后台自动处理 有没有一种简单的方法可以在不设置http.Client对象上的CheckRedirect字段的情况下确定最终URL是什么 我想我应该提一下,我想我想出了一个解决办法,但这有点太草率了,因为它涉及到使用全局变量和在自定义http.Client上设置CheckRedirect字段 必须有一个更干净的方法来做这件事。我希望这样: package main impo

所以,我使用的是net/http包。我得到一个网址,我知道肯定是重定向。它甚至可能在登录到最终URL之前重定向几次。重定向在后台自动处理

有没有一种简单的方法可以在不设置http.Client对象上的CheckRedirect字段的情况下确定最终URL是什么

我想我应该提一下,我想我想出了一个解决办法,但这有点太草率了,因为它涉及到使用全局变量和在自定义http.Client上设置CheckRedirect字段

必须有一个更干净的方法来做这件事。我希望这样:

package main

import (
  "fmt"
  "log"
  "net/http"
)

func main() {
  // Try to GET some URL that redirects.  Could be 5 or 6 unseen redirections here.
  resp, err := http.Get("http://some-server.com/a/url/that/redirects.html")
  if err != nil {
    log.Fatalf("http.Get => %v", err.Error())
  }

  // Find out what URL we ended up at
  finalURL := magicFunctionThatTellsMeTheFinalURL(resp)

  fmt.Printf("The URL you ended up at is: %v", finalURL)
}
输出:

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

很抱歉,我没有一个真正的URL供您使用。这是为我的工作和网站我的工作需要凭据等可能的副本
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