我使用fiddler并且所有参数,变量,formfeild,cookies等都与使用浏览器相同。遗憾的是,重定向到结果页面时似乎丢失了这些值。
protected string WebURL = @"https://webapps.mvd.newmexico.gov/ONLINEDLRENEWAL/Default.aspx";
protected void Button1_Click(object sender, EventArgs e)
{
// first, request the login form to get the viewstate value
//HttpWebRequest webRequest = WebRequest.Create(WebURL) as HttpWebRequest;
//// HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
//StreamReader responseReader = new StreamReader(
// resp.GetResponseStream()
// );
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest webRequest = WebRequest.Create(WebURL) as HttpWebRequest;
webRequest.CookieContainer = cookieContainer;
StreamReader responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
string responseData = responseReader.ReadToEnd();
responseReader.Close();
// extract the viewstate value and build out POST data
string viewState = ExtractFormField(responseData, "__VIEWSTATE");
string EVENTTARGET = ExtractFormField(responseData, "__EVENTTARGET");
string EVENTARGUMENT = ExtractFormField(responseData, "__EVENTARGUMENT");
string VIEWSTATEENCRYPTED = ExtractFormField(responseData, "__VIEWSTATEENCRYPTED");
string EVENTVALIDATION = ExtractFormField(responseData, "__EVENTVALIDATION");
string ToolkitScriptManager1 = ExtractFormField(responseData, "MainContent_ToolkitScriptManager1_HiddenField");
string postData = "";
postData = "MainContent_ToolkitScriptManager1_HiddenField=" + ";;AjaxControlToolkit, Version=4.1.60919.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e:en-US:ee051b62-9cd6-49a5-87bb-93c07bc43d63:5546a2b:475a4ef5:effe2a26:2a35a54f:4355a41:3ac3e789"; // ToolkitScriptManager1;
postData += "&__EVENTTARGET=" + EVENTTARGET;
postData += "&__EVENTARGUMENT=" + EVENTARGUMENT;
postData += "&__VIEWSTATE=" + viewState;
postData += "&__EVENTVALIDATION=" + EVENTVALIDATION;
postData += "&ctl00$MainContent$txtDLNo=999999999";
postData += "&ctl00$MainContent$MaskedEditExtender1_ClientState=";
postData += "&ctl00$MainContent$txtDOB=11/24/1987";
postData += "&ctl00$MainContent$MaskedEditExtender5_ClientState=";
postData += "&ctl00$MainContent$txtLast4OfSSN=9999";
postData += "&ctl00$MainContent$btnSearch=Renew My License/ID";
// now post to the login form
webRequest = WebRequest.Create(WebURL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.CookieContainer = cookieContainer; // cookies;
webRequest.AllowAutoRedirect = true;
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
// we don't need the contents of the response, just the cookie it issues
responseReader = new StreamReader(
webRequest.GetResponse().GetResponseStream()
);
responseData = responseReader.ReadToEnd();
// now we can send out cookie along with a request for the protected page
webRequest = WebRequest.Create(WebURL) as HttpWebRequest;
webRequest.CookieContainer = cookieContainer;
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
// and read the response
responseData = responseReader.ReadToEnd();
responseReader.Close();
litResponse.Text += responseData.Replace("<", "<").Replace(">", ">");
//Response.Write(responseData);
}