C#抓取页面时候,获取页面跳转后的地址

时间:2023-03-09 18:34:43
C#抓取页面时候,获取页面跳转后的地址
static string fanhuiurl(string cahxunurl)
{
string url = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cahxunurl);
req.Method = "HEAD";
req.AllowAutoRedirect = false;
HttpWebResponse myResp = (HttpWebResponse)req.GetResponse();
if (myResp.StatusCode == HttpStatusCode.Redirect)
{ url = myResp.GetResponseHeader("Location"); }
return url;
}