如何防止WebClient类自动跟踪标题中的位置?

时间:2011-07-17 21:27:45

标签: c# .net header location webclient

可以在WebClient类上使用吗?

E.g。类似的东西:

MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest) 

1 个答案:

答案 0 :(得分:22)

您可以编写自定义Web客户端并启用此功能:

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

然后:

using (var client = new WebClientEx())
{
    Console.WriteLine(client.DownloadString("http://google.com"));
}