如何防止 WebClient 类自动跟随标头中的位置?
Posted
技术标签:
【中文标题】如何防止 WebClient 类自动跟随标头中的位置?【英文标题】:How do you prevent the WebClient class from automatically following the location in headers? 【发布时间】:2011-10-07 07:12:22 【问题描述】:在 WebClient 类上可以吗?
例如类似:
MyWebClient.AllowAutoRedirect = false; (of HttpWebRequest)
【问题讨论】:
【参考方案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"));
【讨论】:
感谢分享,定制客户端是个好主意。以上是关于如何防止 WebClient 类自动跟随标头中的位置?的主要内容,如果未能解决你的问题,请参考以下文章