C # post submission method

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.

The following three methods are used: 1. webbrowser is a "mini" browser, 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:

View code

Htmlelement clickbtn = NULL;
If (E. url. tostring (). tolower (). indexof ("http://hi.baidu.com/srxljl")> 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 that it controls cookie and runs all the WebClient in the background, it also provides the ability of asynchronous operations, so that it is convenient to concurrent multiple tasks, and then wait for the results to be returned, and then process them one by one. The code for asynchronous multi-task invocation is as follows:

View code

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://hi.baidu.com/srxljl"); // 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://hi.baidu.com/srxljl"), "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:

View code

Private string postwebrequest (string posturl, string paramdata, encoding dataencode)
{
String ret = string. empty;
Try
{
Byte [] bytearray = dataencode. getbytes (paramdata); // convert
Httpwebrequest webreq = (httpwebrequest) webrequest. Create (New uri (posturl ));
Webreq. method = "Post ";
Webreq. contenttype = "application/X-WWW-form-urlencoded ";

Webreq. contentlength = bytearray. length;
Stream newstream = webreq. getrequeststream ();
Newstream. Write (bytearray, 0, bytearray. Length); // write Parameters
Newstream. Close ();
Httpwebresponse response = (httpwebresponse) webreq. getresponse ();
Streamreader sr = new streamreader (response. getresponsestream (), encoding. Default );
Ret = Sr. readtoend ();
Sr. Close ();
Response. Close ();
Newstream. Close ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Return ret;
}

Httpwebrequest implementation, a simple test program, wrote a function to call a third-party interface to send and receive text messages in a recent project, that is, the function to send messages to mobile phones through a website, at the same time, only the URL, parameter list, and return value are provided in the third-party documents. this function can be easily implemented through the httpwebrequest class of C.

This program is used as a test tool.

This test program is made up of dynamic parameters, that is, different URLs and parameters can be set according to different service providers. However, the post service must be supported.

Of course, some service interfaces are constructed by constructing an XML file and then submitting the XML file service form.

It is also very easy to add support for the construction of XML format, the form is similar, not discussed here, by building an XML file and then post the article, please refer to here post submit XML.

 

 

 

Add parameters and corresponding values. Sensitive information can be encrypted before being sent. The encryption algorithm calls the system MD5 algorithm. Currently, the MD5 algorithm is relatively secure.

For example:

Third-party service interfaces are similar: http://www.cnblogs.com/service? User = xxx & pass = yyy

To call the service to submit post data (note: this is not a real service interface, but an explanation is provided ).

1. In the test tool, you only need to enter

Service URL: http://www.cnblogs.com/serveice

2

. Set the parameter list

Parameter Name: user; Value: XXX;

Parameter Name: pass; Value: YYY;

......

3. Just submit. Different services may return different information. Here, only the return string is used.

 

 

 

Httpwebrequest is simple and convenient to implement. This method is widely used in WebService.

In addition, similar methods can be implemented in automatic logon, but the verification code is not resolved yet, but it can also be implemented through httpwebrequest.

Method for retrieving images:

Httpwebresponse resp; httpwebrequest Req = (httpwebrequest) httpwebrequest. create ("http://www.csdn.net/Images/logo_csdn.gif"); req. timeout = 150000; resp = (httpwebresponse) req. getresponse (); system. drawing. image IMG; IMG = new system. drawing. bitmap (resp. getresponsestream ());

 

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. visit the pages 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 in the accessed page. 5. After the submission is successful, click "Stop interception" in visual sniffer. 6. Click the plus sign in the left column of visual sniffer,

The right side is the content it intercepts:

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 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

 

 

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.