Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

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

Is it possible on the WebClient class?

E.g. something like:

MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest) 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...