开发者

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

开发者 https://www.devze.com 2023-03-20 18:20 出处:网络
Is it possible on the WebClient class? E.g. something like: MyWebClient开发者_C百科.AllowAutoRedirect = false; (of HttpWebRequest)

Is it possible on the WebClient class?

E.g. something like:

MyWebClient开发者_C百科.AllowAutoRedirect = false; (of HttpWebRequest) 


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

精彩评论

暂无评论...
验证码 换一张
取 消