Using HttpWebRequest to submit data to a Web site

Source: Internet
Author: User

Using HttpWebRequest to submit data to a Web site

Http://www.blogjava.net/killme2008/archive/2011/05/20/105023.html

Transferred from: http://www.cnblogs.com/webman/archive/2006/11/17/564106.html

HttpWebRequest is a class in the. NET base Class library that allows users to interact with the server through the HTTP protocol, under the namespace System.Net.

HttpWebRequest the HTTP protocol is fully encapsulated, the HTTP protocol Header, Content, cookies have done the properties and methods of support, it is easy to write a simulation browser automatic login program.

The program uses HTTP protocol and server interaction mainly for data submission, usually the data is submitted through GET and POST two ways to complete, the following two ways to explain:

1. GET mode. The GET method completes the data submission by appending parameters to the network address, for example, in address Http://www.google.com/webhp?hl=zh-CN, the previous section http://www.google.com/webhp represents the URL of the data submission, The following section HL=ZH-CN represents the additional parameters, where HL represents a key (key) and ZH-CN represents the value of the key (value). The program code is as follows:

HttpWebRequest req = (HttpWebRequest) httpwebrequest.create ("Http://www.google.com/webhp?hl=zh-CN");
Req. Method = "GET";
using (WebResponse WR = req. GetResponse ())
{
Handle the content of the received page here
}


2. POST mode. The POST method completes the data submission by filling in the parameters in the page content, the format of the parameter is the same as the GET method, which is similar to the structure of hl=zh-cn&newwindow=1. The program code is as follows:


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 ())
{
Handle the content of the received page here
}



In the above code, we visited the URL of www.google.com, submitted the data in the form of GET and POST respectively, and received the content of the returned page. However, if the submitted parameter contains Chinese, then this processing is not enough, need to encode it, so that the other site can be recognized.

3. Use GET mode to submit Chinese data. GET method to complete the data submission by attaching parameters in the network address, for the Chinese encoding, commonly used gb2312 and UTF8 two kinds of code, with gb2312 way to access the program codes as follows:

Encoding myencoding = encoding.getencoding ("gb2312");
string address = "http://www.baidu.com/s?" + httputility.urlencode ("parameter One", myencoding) + "=" + Httputility.urlencode ("Value One") , myencoding);
HttpWebRequest req = (HttpWebRequest) httpwebrequest.create (address);
Req. Method = "GET";
using (WebResponse WR = req. GetResponse ())
{
Handle the content of the received page here
}



In the above program code, we get access to the URL http://www.baidu.com/s, passed the parameter "parameter one = value One", because unable to tell the other party to submit data encoding type, so the encoding method to the other side of the site as standard. Common site, www.baidu.com (Baidu) encoding method is gb2312, www.google.com (google) encoding is UTF8.

4. Use POST to submit Chinese data. The POST method completes the data submission by filling in the parameter in the page content, because the submitted parameters can explain the encoding method used, so it is theoretically possible to achieve greater compatibility. The program code that is accessed using the Gb2312 method is as follows:

Encoding myencoding = encoding.getencoding ("gb2312");
string param = Httputility.urlencode ("parameter One", myencoding) + "=" + Httputility.urlencode ("Value One", myencoding) + "&" + Http Utility.urlencode ("parameter Two", myencoding) + "=" + Httputility.urlencode ("Value two", myencoding);

byte[] postbytes = Encoding.ASCII.GetBytes (param);

HttpWebRequest req = (HttpWebRequest) httpwebrequest.create ("http://www.baidu.com/s");
Req. Method = "POST";
Req. ContentType = "application/x-www-form-urlencoded;charset=gb2312";
Req. ContentLength = Postbytes.length;

using (Stream Reqstream = req. GetRequestStream ())
{
Reqstream.write (postbytes, 0, Postbytes. Length);
}
using (WebResponse WR = req. GetResponse ())
{
Handle the content of the received page here
}



From the above code can be seen, the POST Chinese data, the first time using the UrlEncode method to convert the medium character to the encoded ASCII code, and then submitted to the server, the submission can be described in the way of encoding, to enable the other server to correctly parse.

The above lists the cases where the client program interacts with the server using the HTTP protocol, which is commonly used for GET and POST methods. Now the popular WebService also interacts with the HTTP protocol, using the POST method. Slightly different from the above, the data content submitted by WebService and the data received are encoded using XML. Therefore, HttpWebRequest can also be used in cases where the WebService is called.

Using HttpWebRequest to submit data to a website (go)

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.