Simulated Web Access with login and verification code to fetch data after login
1 Verification Code1 Place a PictureBox (imgvalidate) on the form to store the obtained captcha picture,
2 using the browser's developer tool Firefox (F12) to analyze the URL of the verification code
private void Getvalidateimage ()
{
cookies = new Cookiecontainer ();
String strURL = "http://www.xxx.com/ValidateCodePicture.aspx?Key=" +strvalidcode; Verification code page Strvalidcode this random code must be taken out first.
Cookiecontainer cc = new Cookiecontainer ();
HttpWebRequest request = (HttpWebRequest) webrequest.create (strURL);
Set Request args
Request. Method = "Get";
Request. Cookiecontainer = CC;
Request. KeepAlive = true;
Request. ContentType = "application/x-www-form-urlencoded; Charset=utf-8 ";
Request. ContentType = "text/html";
Analog Goole Browser Access
Request. UserAgent =
"mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.95 safari/537.36 ";
Request. Referer = strURL;
Request. Headers.add ("X-requested-with:xmlhttprequest");
Request. Headers.add (Httprequestheader.acceptlanguage, "zh-cn,zh;q=0.8,en;q=0.6,nl;q=0.4,zh-tw;q=0.2");
Request. ContentLength = Postdatabyte.length; text/html; Charset=utf-8
Request. Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Request. Automaticdecompression = Decompressionmethods.deflate | Decompressionmethods.gzip |
Decompressionmethods.none;
Support the jump page, the query results will be the page after the jump
Request. AllowAutoRedirect = true;
Request. Headers.add ("accept-encoding", "gzip, deflate");
if (Request. Method = = "POST")
{
(Request as HttpWebRequest). ContentType = "application/x-www-form-urlencoded";
}
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
MemoryStream ms = NULL;
using (var stream = response. GetResponseStream ())
{
byte[] buffer = new Byte[response. ContentLength];
int offset = 0, actuallyread = 0;
Do
{
Actuallyread = stream. Read (buffer, offset, buffer. Length-offset);
Offset + = Actuallyread;
}
while (Actuallyread > 0);
ms = new MemoryStream (buffer);
}
Response. Close ();
cookies = Request. Cookiecontainer; Save cookies
Strcookies = Request. Cookiecontainer.getcookieheader (Request. RequestUri); Convert cookies into strings
Bitmap SOURCEBM = new Bitmap ((Stream) MS);//Initialize BITMAP image
Imgvalidate.image = SOURCEBM;
}
2 The content of JS assignment Some Web pages do not see the value of the control in the way the source code of the Web page, you need to use the following method
That is, C # comes with the Webbrowse to load the Web page, and then use Webbrowser1.document to fetch the value of the corresponding control, such as
Tring STRMSG2 = webBrowser1.Document.GetElementById ("Hdvalidatecodeid"). outerhtml;
3 Get the parameters to submit
If it is an ASP. Web page and submit "__eventtarget", "__eventargument", "__viewstate" The three parameters, this can also be seen in the developer tools-network-parameters
You can use HttpRequest first to obtain the source code and then separate precipitation
This is webbrowse in Riga.
private void Getviewstate ()
{
String strMsg = WebBrowser1.Document.GetElementById ("__viewstate"). outerhtml;
Fetch ViewState value
<input id=__viewstate Type=hidden value=/wepdwukmtg0ntk3mjg2n2rk name=__viewstate>
MatchCollection mc = regex.matches (STRMSG, "id=__viewstate.* (? <viewstate>value[^>]*)", Regexoptions.ignorecase);
if (MC. Count > 0)
{
foreach (Match m in MC)
{
Strviewstate = m.groups["ViewState"]. Value.tostring (). Trim ();
if (Strviewstate.length > 0)
{
Strviewstate = Strviewstate.replace ("value=", ""). Replace ("\" "," "). Replace ("\ \", ""). Replace ("Name=__viewstate", ""). Replace ("", "");
}
}
}
<input Id=hdvalidatecodeid Type=hidden VALUE=C1B52D3A-1F8B-1DC4-0D44-32A4B46EF8AF name=hdValidateCodeID>
String strMsg2 = WebBrowser1.Document.GetElementById ("Hdvalidatecodeid"). outerhtml;
MatchCollection MC2 = regex.matches (STRMSG2, "id=hdvalidatecodeid.* (? <validatecode>value[^>]*)", Regexoptions.ignorecase);
if (MC2. Count > 0)
{
foreach (Match m in mc2)
{
Strvalidcode = m.groups["Validatecode"]. Value.tostring (). Trim ();
if (Strvalidcode.length > 0)
{
Strvalidcode = Strvalidcode.replace ("value=", ""). Replace ("\" "," "). Replace ("\ \", ""). Replace ("/", ""). Replace ("Name=hdvalidatecodeid", ""). Replace ("", "");
}
}
}
Txtvalidcode.text = Strvalidcode;
Txtviewstate.text = strviewstate;
The cookie of String is to be converted into a cookie type and put into Cookiecontainer
string cookiestr = WebBrowser1.Document.Cookie;
string[] Cookstr = Cookiestr.split (';');
foreach (String str in COOKSTR)
{
Try
{
string[] Cookienamevalue = str. Split (' = ');
Cookie CK = new Cookie (cookienamevalue[0). Trim (). ToString (), cookienamevalue[1]. Trim (). ToString ());
Ck. Domain = "xxx.com"; Must write to
Mycookiecontainer.add (CK);
}
Catch
{
}
}
}
3 Login and Access cookie submission parameters, and cookies for subsequent use
private void Login ()
{
cookies = new Cookiecontainer ();
String strURL = "Http://www.xxx.com/Login.aspx"; Verification code Page
HttpWebRequest request = (HttpWebRequest) webrequest.create (strURL);
Set Request args
Request. Method = "POST";
Request. Cookiecontainer = Mycookiecontainer;
Request. KeepAlive = true;
Request. ContentType = "application/x-www-form-urlencoded; Charset=utf-8 ";
Request. ContentType = "text/html";
Analog Goole Browser Access
Request. UserAgent =
"mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.95 safari/537.36 ";
Request. Referer = strURL;
Request. Headers.add ("X-requested-with:xmlhttprequest");
Request. Headers.add (Httprequestheader.acceptlanguage, "zh-cn,zh;q=0.8,en;q=0.6,nl;q=0.4,zh-tw;q=0.2");
Request. ContentLength = Postdatabyte.length; text/html; Charset=utf-8
Request. Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
Request. Automaticdecompression = Decompressionmethods.deflate | Decompressionmethods.gzip |
Decompressionmethods.none;
Support the jump page, the query results will be the page after the jump
Request. AllowAutoRedirect = true;
Request. Headers.add ("accept-encoding", "gzip, deflate");
if (Request. Method = = "POST")
{
(Request as HttpWebRequest). ContentType = "application/x-www-form-urlencoded";
}
---begin
String postdata = String. Format ("txtusername={0}&txtpassword={1}&txtvalidatecode={2}&hdvalidatecodeid={3}&ddllanguage= cn&btnlogin= Login &__eventtarget=&__eventargument=&__viewstate={4} ", TxtUserName.Text, txtPassword.Text, txtvalidate.text,strvalidcode,strviewstate); This is done in accordance with the post string found in the previous firebug.
byte[] Postdatabyte = Encoding.UTF8.GetBytes (postdata);
Request. ContentLength = Postdatabyte. Length;
using (Stream stream = Request. GetRequestStream ())
{
Stream. Write (postdatabyte, 0, Postdatabyte. Length);
}
---end---
HttpWebResponse response = (HttpWebResponse) request. GetResponse ();
StreamReader reader = new StreamReader (response. GetResponseStream (), encoding.getencoding ("gb2312"));
StreamReader reader = new StreamReader (response. GetResponseStream (), Encoding.UTF8);
String STRMSG = reader. ReadToEnd ();
Response. Close ();
cookies = Request. Cookiecontainer; Save cookies, and then request other Web pages to use this cookie without having to log in
Lblogin.text = "Logged in";
Btnsearchresume.enabled = true;
}
Simulated Web Access with login and verification code to fetch data after login