2017-07-07 70 views

回答

1

您需要检查HTTP响应中包含的HTTP状态,如果它是HTTP“302 Found”,则需要从响应中获取“位置”标头的值。该值将成为重定向的目标,因此您需要下载目标。

String content; 
try 
{ 
    content = new System.Net.WebClient().DownloadString(page); 
} 
catch(WebException e) 
{ 
    HttpWebResponse response = (System.Net.HttpWebResponse)we.Response; 
    ... examine status, get headers, etc ... 
} 
+0

这我知道,但我不知道该怎么办。 – MiscellaneousUser

0

下面是它是如何使用的HttpClient

 string Page = "https://stackoverflow.com/questions/44980231/"; 
     HttpClientHandler ClientHandler = new HttpClientHandler(); 
     ClientHandler.AllowAutoRedirect = false; 
     HttpClient client = new HttpClient(ClientHandler); 
     HttpResponseMessage response = await client.GetAsync(Page); 
     try 
     { 
      string location = response.Headers.GetValues("Location").FirstOrDefault(); 
      if (!Uri.IsWellFormedUriString(location, UriKind.Absolute)) 
      { 
       Uri PageUri = new Uri(Page); 
       location = PageUri.Scheme + "://" + PageUri.Host + location; 
      } 
      MessageBox.Show(location); 
     } 
     catch 
     { 
      MessageBox.Show("No redirect!"); 
     } 

结果做:

Result

相关问题