您可能必须使用 WebClient 进行声明登录,看看您是否可以以此为起点。
using (var webClient = new WebClient()) {
string url = "http://yoursite";
string result = null;
try {
result = webClient.DownloadString(url);
} catch (Exception ex) {
if (ex.ToString().Contains("403")) {
result = webClient.DownloadString(url + "/_forms/default.aspx");
string viewstate = result.Substring(result.IndexOf("__VIEWSTATE") + 11);
viewstate = viewstate.Substring(viewstate.IndexOf("value=\"") + 7);
viewstate = viewstate.Substring(0, viewstate.IndexOf("\""));
string eventvalidation = result.Substring(result.IndexOf("__EVENTVALIDATION") + 17);
eventvalidation = eventvalidation.Substring(eventvalidation.IndexOf("value=\"") + 7);
eventvalidation = eventvalidation.Substring(0, eventvalidation.IndexOf("\""));
System.Collections.Specialized.NameValueCollection values = new System.Collections.Specialized.NameValueCollection();
values.Add("__EVENTARGUMENT", "");
values.Add("__EVENTTARGET", "");
values.Add("__EVENTVALIDATION", eventvalidation);
values.Add("__LASTFOCUS", viewstate);
values.Add("__VIEWSTATE", "");
values.Add("ctl00$PlaceHolderMain$signInControl$UserName", "");
values.Add("ctl00$PlaceHolderMain$signInControl$login", "Sign In");
values.Add("ctl00$PlaceHolderMain$signInControl$password", "");
//byte[] data = webClient.UploadValues(url + "/_forms/default.aspx", "POST", values);
//result = System.Text.Encoding.Default.GetString(data);
//result = webClient.UploadString(url + "/_forms/default.aspx?__EVENTARGUMENT=&__EVENTTARGET=&__EVENTVALIDATION=" +
// eventvalidation + "&__LASTFOCUS=&__VIEWSTATE=" + viewstate +
// "&ctl00$PlaceHolderMain$signInControl$UserName=&ctl00$PlaceHolderMain$signInControl$login=Sign+In&ctl00$PlaceHolderMain$signInControl$password=", "");
string location = webClient.ResponseHeaders["Location"];
result = webClient.DownloadString(url);
}
}
}