C # three methods for simulating automatic logon and submitting POST information,

Source: Internet
Author: User

C # three methods for simulating automatic logon and submitting POST information,

Automatic Webpage Logon (submitting Post content) is widely used, such as identity verification, program upgrade, and online voting. The following methods are implemented using C.
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 ("xxx.htm")> 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://xxxx.com.cn/tp.asp? Id = 129 "); // open the 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://xxxxxxy.com.cn/tp.asp? Id = 129 ")," 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. Therefore, Cookie operations are 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://www.xxxx.com/chk.asp "));
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 also provides asynchronous operations. If you are interested, it is not difficult to check MSDN.

 


In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

In the C language, what is the symbol (->) and how to use it?

This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.

Related Article

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.