【问题标题】:How do you prevent the WebClient class from automatically following the location in headers?如何防止 WebClient 类自动跟随标头中的位置?
【发布时间】:2011-10-07 07:12:22
【问题描述】:

在 WebClient 类上可以吗?

例如类似:

MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest) 

【问题讨论】:

    标签: c# .net header location webclient


    【解决方案1】:

    您可以编写一个自定义 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"));
    }
    

    【讨论】:

    • 感谢分享,定制客户端是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2010-09-12
    • 2017-03-10
    • 2011-09-27
    • 1970-01-01
    • 2011-01-26
    相关资源
    最近更新 更多