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

被刻印的时光 ゝ 提交于 2019-11-30 07:42:52

问题


Is it possible on the WebClient class?

E.g. something like:

MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest) 

回答1:


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"));
}


来源:https://stackoverflow.com/questions/6726889/how-do-you-prevent-the-webclient-class-from-automatically-following-the-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!