Using WebClient in C# is there a way to get the URL of a site after being redirected?

后端 未结 8 1522
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 03:21

Using the WebClient class I can get the title of a website easily enough:

WebClient x = new WebClient();    
string source = x.DownloadString(s);
string titl         


        
8条回答
  •  伪装坚强ぢ
    2020-11-29 03:57

    HttpWebRequest.AllowAutoRedirect can be set to false. Then you'd have to manually http status codes in the 300 range.

    // Create a new HttpWebRequest Object to the mentioned URL.
    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");    
    myHttpWebRequest.MaximumAutomaticRedirections=1;
    myHttpWebRequest.AllowAutoRedirect=true;
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();  
    

提交回复
热议问题