How do you prevent the WebClient class from automatically following the location in headers?

后端 未结 1 1300
无人及你
无人及你 2020-12-17 06:09

Is it possible on the WebClient class?

E.g. something like:

MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest) 
相关标签:
1条回答
  • 2020-12-17 06:44

    You could write a custom web client and enable this functionality:

    public class WebClientEx : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = (HttpWebRequest)base.GetWebRequest(address);
            request.AllowAutoRedirect = false;
            return request;
        }
    }
    

    and then:

    using (var client = new WebClientEx())
    {
        Console.WriteLine(client.DownloadString("http://google.com"));
    }
    
    0 讨论(0)
提交回复
热议问题