代码如下:
public class MyWebClient : WebClient
{
public bool AllowAutoRedirect { get; set; } = true;
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
((HttpWebRequest)request).AllowAutoRedirect = this.AllowAutoRedirect;
}
return request;
}
}
说明:
AllowAutoRedirect这个属性表示是否允许自动重定向,存在于HttpWebRequest类中
WebClient没有对外开放这个属性,我们可以通过继承WebClient,重写GetWebRequest来开放这个属性
MSDN:
https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspx