代碼以下:web
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; } }
說明:ide
AllowAutoRedirect這個屬性表示是否容許自動重定向,存在於HttpWebRequest類中this
WebClient沒有對外開放這個屬性,咱們能夠經過繼承WebClient,重寫GetWebRequest來開放這個屬性spa
MSDN:.net
https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest.allowautoredirect(v=vs.110).aspxcode