1
有沒有這樣的事從Java:HTTPClient - Capture a list of all Redirects如何獲得使用HttpClient的所有重定向的URL列表C#
爲C#的HttpClient?
有沒有這樣的事從Java:HTTPClient - Capture a list of all Redirects如何獲得使用HttpClient的所有重定向的URL列表C#
爲C#的HttpClient?
可以使用HttpWebRequest做到這一點:
public static string GetRedirectedUrls(string url)
{
StringBuilder sb = new StringBuilder();
while (!string.IsNullOrWhiteSpace(url))
{
sb.AppendLine(url);
HttpWebRequest request = HttpWebRequest.CreateHttp(url);
request.AllowAutoRedirect = false;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
url = response.GetResponseHeader("Location");
}
}
return sb.ToString();
}