2016-06-09 25 views
0

我使用Go http包開發了一個代碼,以便我可以獲取任何url的狀態碼。但經過一番挖掘後,我檢查了一下爲什麼請求花了很長時間,並且我發現URL一直在重定向,直到狀態200 OK。如何獲取第一個HTTP(重定向)響應?

我想管理從Web服務器獲得第一個響應。我不想總是200行,不管它給我我想要的。我該怎麼辦?

示例: https://apple.com已回覆301當我使用cURL來獲取代碼時,它是永久移動的。 這是我想要的響應,而不是重定向到200.

+0

另請參閱:http://stackoverflow.com/questions/23297520/how-can-i-make-the-go-http-client-not-follow-redirects-automatically http://stackoverflow.com/問題/ 35089052 /如何獲取重定向網址而不是頁面內容在golang –

+0

YEP鏈接使它 – Elliott

回答

1

http.Client提供了一種通過CheckRedirect成員來控制該響應的可能性。

從上面的鏈接:

// CheckRedirect specifies the policy for handling redirects. 
// If CheckRedirect is not nil, the client calls it before 
// following an HTTP redirect. The arguments req and via are 
// the upcoming request and the requests made already, oldest 
// first. If CheckRedirect returns an error, the Client's Get 
// method returns both the previous Response and 
// CheckRedirect's error (wrapped in a url.Error) instead of 
// issuing the Request req. 
// 
// If CheckRedirect is nil, the Client uses its default policy, 
// which is to stop after 10 consecutive requests. 
ct func(req *Request, via []*Request) error 

聲明:未測試。

相關問題