How to Learn and analyze data submission methods when using HttpWebRequest in c #

Source: Internet
Author: User

C # before learning how to submit data in HttpWebRequest, let's take a look at HttpWebRequest.. net base class library, in The namespace System.. Net is used to allow users to interact with the server through the HTTP protocol.

 

C # before learning how to submit data in HttpWebRequest, let's take a look at HttpWebRequest.. net base class library, in The namespace System.. Net is used to allow users to interact with the server through the HTTP protocol.

C # functions of HttpWebRequest:

HttpWebRequest completely encapsulates the HTTP protocol and supports attributes and methods for headers, Content, and cookies in the HTTP protocol, it is easy to compile a program that simulates automatic browser logon.

C # HttpWebRequest:

The program uses HTTP protocol and server interaction mainly for data submission. Generally, data submission is done through GET and POST methods. The following two methods are described below:

 C # HttpWebRequest: data submission method 1. GET method.

GET method to complete data submission by attaching parameters in the network address, such as in the address http://www.google.com/webhp? In hl = zh-CN, the previous part of the http://www.google.com/webhp represents the URL for data submission, and the latter part of hl = zh-CN represents the additional parameters, where hl represents a key ), zh-CN indicates the value corresponding to this key ). The program code is as follows:

 

The following is a code snippet:
HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create ("http://www.google.com/webhp? Hl = zh-CN ");
Req. Method = "GET ";
Using (WebResponse wr = req. GetResponse ())
{
// Process the received page content here
}

 

C # HttpWebRequest: data submission method 2. POST method.

The POST method submits data by filling in parameters in the page content. The format of parameters is the same as that of the GET method, which is similar to the structure such as hl = zh-CN & newwindow = 1. The program code is as follows:

 

The following is a code snippet:
String param = "hl = zh-CN & newwindow = 1 ";
Byte [] bs = Encoding. ASCII. GetBytes (param );
HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create ("http://www.google.com/intl/zh-CN ");
Req. Method = "POST ";
Req. ContentType = "application/x-www-form-urlencoded ";
Req. ContentLength = bs. Length;
Using (Stream reqStream = req. GetRequestStream ())
{
ReqStream. Write (bs, 0, bs. Length );
}
Using (WebResponse wr = req. GetResponse ())
{
// Process the received page content here
}

 

In the above Code, we visited www.google.com, submitted data in GET and POST methods, and received the returned page content. However, if the submitted parameters contain Chinese characters, such processing is not enough. You need to encode the parameters so that the other website can recognize them.

C # HttpWebRequest data submission method 3. Use GET to submit Chinese data.

The GET method adds parameters to the network address to complete data submission. For Chinese encoding, the commonly used two types are gb2312 and utf8. The program code accessed by gb2312 encoding is as follows:

 

The following is a code snippet:
Encoding myEncoding = Encoding. GetEncoding ("gb2312 ");
String address = "http://www.baidu.com/s? "+ HttpUtility. UrlEncode (" parameter 1 ", myEncoding) +" = "+ HttpUtility. UrlEncode (" value 1 ", myEncoding );
HttpWebRequest req = (HttpWebRequest) HttpWebRequest. Create (address );
Req. Method = "GET ";
Using (WebResponse wr = req. GetResponse ())
{
// Process the received page content here
}

 

In the above program code, we access the URL http://www.baidu.com/s in GET mode, passed the parameter "parameter 1 = value 1", because the other party cannot tell the submitted data encoding type, therefore, the encoding method should be based on the website of the other party. In common websites, www.baidu.com (Baidu) is encoded in gb2312, and www.google.com (Google) is encoded in utf8.

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.