C # multiple methods for Automatic Webpage logon and POST submission

Source: Internet
Author: User

The core of Automatic Webpage logon and POST submission is to analyze the source code (HTML) of the webpage. in C #, there are many HTML components that can be used to extract the webpage, webBrowser, WebClient, and HttpWebRequest are commonly used.

These three methods are used as follows:
1. WebBrowser is a "mini" browser, which features no Cookie or built-in JS issues during Post.
WebBrowser is a new component provided by VS2005 (in fact, it encapsulates the IE interface). The POST function is generally implemented by analyzing HtmlDocument in DocumentCompleted of webBrowser. The Code is as follows:

HtmlElement ClickBtn = null;
If (e. Url. ToString (). ToLower (). IndexOf ("http://sandou.cnblogs.com/")> 0) // login page
{
HtmlDocument doc = webBrowser1.Document;
For (int I = 0; I <doc. All. Count; I ++)
{
If (doc. All [I]. TagName. ToUpper (). Equals ("INPUT "))
{
Switch (doc. All [I]. Name)
{
Case "userCtl ":
Doc. All [I]. InnerText = "user01 ";
Break;
Case "passCt1 ":
Doc. All [I]. InnerText = "mypass ";
Break;
Case "B1 ":
ClickBtn = doc. All [I]; // submit button
Break;
}
}
}
ClickBtn. InvokeMember ("Click"); // Click the button
}

2. WebClient encapsulates some HTTP classes and has simple operations. Compared with webBrowser, WebClient features self-configured proxy. Its disadvantage is COOKIE control.
The WebClient runs in the background and provides the ability to perform asynchronous operations. This facilitates concurrent tasks and then waits for the results to be returned and then processes them one by one. The code for asynchronous multi-task invocation is as follows:

Private void StartLoop (int ProxyNum)
{
WebClient [] wcArray = new WebClient [ProxyNum]; // Initialization
For (int idArray = 0; idArray <ProxyNum; idArray ++)
{
WcArray [idArray] = new WebClient ();
WcArray [idArray]. OpenReadCompleted + = new OpenReadCompletedEventHandler (Pic_OpenReadCompleted2 );
WcArray [idArray]. UploadDataCompleted + = new UploadDataCompletedEventHandler (Pic_UploadDataCompleted2 );
Try
{

WcArray [idArray]. Proxy = new WebProxy (proxy [1], port );
WcArray [idArray]. OpenReadAsync (new Uri ("http://sandou.cnblogs.com/"); // open WEB;
Proxy = null;
}
Catch
{
}
}
}

Private void Pic_OpenReadCompleted2 (object sender, OpenReadCompletedEventArgs e)
{
If (e. Error = null)
{
String textData = new StreamReader (e. Result, Encoding. Default). ReadToEnd (); // obtain the returned information
..
String cookie = (WebClient) sender). ResponseHeaders ["Set-Cookie"];
(WebClient) sender). Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
(WebClient) sender). Headers. Add ("Accept-Language", "zh-cn ");
(WebClient) sender). Headers. Add ("Cookie", cookie );

String postData = ""
Byte [] byteArray = Encoding. UTF8.GetBytes (postData); // convert it to a binary array
(WebClient) sender). UploadDataAsync (new Uri ("http://sandou.cnblogs.com/"), "POST", byteArray );
}
}

Private void Pic_UploadDataCompleted2 (object sender, UploadDataCompletedEventArgs e)
{
If (e. Error = null)
{
String returnMessage = Encoding. Default. GetString (e. Result );

}
}

 

3. HttpWebRequest is relatively low-level and can implement many functions. Cookie operations are also simple:

 
Private bool PostWebRequest ()
{
CookieContainer cc = new CookieContainer ();
String pos tData = "user =" + strUser + "& pass =" + strPsd;
Byte [] byteArray = Encoding. UTF8.GetBytes (postData); // convert

HttpWebRequest webRequest2 = (HttpWebRequest) WebRequest. Create (new Uri (http://sandou.cnblogs.com /));
WebRequest2.CookieContainer = cc;
WebRequest2.Method = "POST ";
WebRequest2.ContentType = "application/x-www-form-urlencoded ";
WebRequest2.ContentLength = byteArray. Length;
Stream newStream = webRequest2.GetRequestStream ();
// Send the data.
NewStream. Write (byteArray, 0, byteArray. Length); // Write Parameters
NewStream. Close ();

HttpWebResponse response2 = (HttpWebResponse) webRequest2.GetResponse ();
StreamReader sr2 = new StreamReader (response2.GetResponseStream (), Encoding. Default );
String text2 = sr2.ReadToEnd ();

}

HttpWebRequest implementation. This is copied from the internet! I used to log on to WWW. ASP. NET with the relevant code and post it successfully. Unfortunately, the Code does not know where to put it.

HttpWebRequest automatically logs on to the website and obtains the website content (websites without verification codes)
You can use Visual Sniffer (Baidu search) to capture submitted data information:
1. Access the page you need to submit outside site, such as CSDN login page http://www.csdn.net/member/UserLogin.aspx
2. Fill in the required information, such as the user name and password,
3. Open Visual Sniffer and click "Start interception"
4. Submit it on the accessed page.
5. After the submission is successful, stop blocking in Visual Sniffer"
6. Click the plus sign in the left column of Visual Sniffer, And the content intercepted on the right is as follows:

 
POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword, application/x-shockwave-flash ,*/*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. net clr 1.1.4322; InfoPath.1)
Host: www.csdn.net
Content-Length: 355
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC = FMEGGCKDBKHAMMCGKPFDMBFG; ASP. NET_SessionId = lusprmnom05lr445tmteaf55; userid = 699879

_ EVENTTARGET =&__ EVENTARGUMENT =&__ VIEWSTATE = %%%%3d & CSDNUserLogin % 3Atb_UserName = testusername & CSDNUserLogin % 3Atb_Password = testpassword & CSDNUserLogin % login = 9232 & from = % 3AImage_Login.x = 36 & CSDNUserLogin % 3AImage_Login.y = 6
GET http://www.csdn.net/mycustompage.htm? Aspxerrorpath =/member/UserLogin. aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd. ms-excel, application/vnd. ms-powerpoint, application/msword, application/x-shockwave-flash ,*/*
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. net clr 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC = FMEGGCKDBKHAMMCGKPFDMBFG; ASP. NET_SessionId = lusprmnom05lr445tmteaf55; userid = 699879
The above is the interception content. The parameter section of the submitted data (strArgs in the program) is as follows:
_ EVENTTARGET = & __ EVENTARGUMENT = & __ VIEWSTATE = dDwtMTcwMzgxNjQ2Mjs7bDxDU
%% 2Btu
1q2wmRZoAJTi9L73w1zBleylY % 3D & CSDNUserLogin % 3Atb_UserName = testusername & CSDN
UserLogin % 3Atb_Password = testpassword & CSDNUserLogin % 3Atb_ExPwd = 9232

 
Protected static string cookieHeader;
Private void Page_Load (object sender, System. EventArgs e)
{
String strReContent = string. Empty;
// Log on
StrReContent = PostLogin ("http://www.mystand.com.cn/login/submit.jsp page", "submitted parameters: userid = hgj0000 & password = 06045369", "reference address: http://www.mystand.com.cn /");
// Note the parameters passed during asp.net Login
// StrReContent = PostLogin ("http://www.mystand.com.cn/login.aspx ", "_ VIEWSTATE = bytes % 2 BPjs % 2 BOzs % 2BOz4% bytes % 2BKbNPsjd7Op % 2Fl26Iw % 3D % 3D & txtUserName = hxf & txtPassword = hxf0000 & btnEnter = % E7 99% BB % E5 % BD % 95 ", http://www.mystand.com.cn/login.aspx ");
// Obtain the page
StrReContent = GetPage ("http://www.mystand.com.cn/company/getdata.jsp? Code = "," reference address: http://www.mystand.com.cn /");
// StrReContent = GetPage ("http://www.mystand.com.cn/Modules/index.aspx", "http://www.mystand.com.cn/login.aspx ");
// Process the obtained content: strReContent
}

/** // <Summary>
/// Function Description: Simulate the logon page, submit the logon data for logon, and record the cookie in the Header
/// </Summary>
/// <Param name = "strURL"> address of the logon data submission page </param>
/// <Param name = "strArgs"> User Logon data </param>
/// <Param name = "strReferer"> reference address </param>
/// <Returns> you can return the page content or not </returns>
Public static string PostLogin (string strURL, string strArgs, string strReferer)
{
String strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest. Create (strURL );
MyHttpWebRequest. AllowAutoRedirect = true;
MyHttpWebRequest. KeepAlive = true;
MyHttpWebRequest. accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd. ms-excel, application/msword, application/x-shockwave-flash ,*/*";
MyHttpWebRequest. Referer = strReferer;

MyHttpWebRequest. UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon;. net clr 2.0.50727 )";
MyHttpWebRequest. ContentType = "application/x-www-form-urlencoded ";
MyHttpWebRequest. Method = "POST ";

CookieCollection myCookies = null;
CookieContainer myCookieContainer = new CookieContainer ();
MyHttpWebRequest. CookieContainer = myCookieContainer;

Stream MyRequestStrearm = myHttpWebRequest. GetRequestStream ();
StreamWriter MyStreamWriter = new StreamWriter (MyRequestStrearm, Encoding. ASCII );
// Write data to the Request stream of HttpWebRequest
MyStreamWriter. Write (strArgs );
// Close the open object
MyStreamWriter. Close ();
MyRequestStrearm. Close ();

HttpWebResponse response = null;
System. IO. StreamReader sr = null;
Response = (HttpWebResponse) myHttpWebRequest. GetResponse ();

CookieHeader = myHttpWebRequest. CookieContainer. GetCookieHeader (new Uri (strURL ));
HttpContext. Current. Application. Lock ();
HttpContext. Current. Application ["cookieHeader"] = cookieHeader;
HttpContext. Current. Application. UnLock ();
MyCookies = response. Cookies;

Sr = new System. IO. StreamReader (response. GetResponseStream (), Encoding. GetEncoding ("gb2312"); // UTF-8
StrResult = sr. ReadToEnd ();
Return strResult;
}

/** // <Summary>
/// Function Description: After successfully logging on to PostLogin, record the cookie in Headers and obtain the content on other pages on this website.
/// </Summary>
/// <Param name = "strURL"> obtain the address of a page on the website. </param>
/// <Param name = "strReferer"> referenced address </param>
/// <Returns> return page content </returns>
Public static string GetPage (string strURL, string strReferer)
{
String strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest. Create (strURL );
MyHttpWebRequest. ContentType = "text/html ";
MyHttpWebRequest. Method = "GET ";
MyHttpWebRequest. Referer = strReferer;
MyHttpWebRequest. Headers. Add ("cookie:" + cookieHeader );

HttpWebResponse response = null;
System. IO. StreamReader sr = null;
Response = (HttpWebResponse) myHttpWebRequest. GetResponse ();
Sr = new System. IO. StreamReader (response. GetResponseStream (), Encoding. GetEncoding ("gb2312"); // UTF-8
StrResult = sr. ReadToEnd ();
Return strResult;
}

Technical Application-Web Page Automatic Logon (submit Post content) is used for many purposes, such as identity verification, program upgrade, and online voting. The following method is implemented using C.

Unsolved problems-currently the biggest problem cannot bypass verification codes-I have discussed image algorithms with my colleagues, which are basically difficult to identify. There are also many examples of recognizing verification codes on the Internet, however, it is still possible for simple noises, but it is useless for complicated ones! So far, I have not passed the test! If you have passed the test, please post the code for our research.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.