[Transfer] C # Get and post data to the web site
Httpwebrequest is a class in the. NET base class library. Under the namespace system. net, it is used to allow users to interact with the server through the HTTP protocol.
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 simulated browser for automatic login.Program.
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:
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 ). ProgramCodeAs follows:
HttpwebrequestReq = (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
}
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:
StringParam ="Hl = ZH-CN & newwindow = 1";
Byte[] BS =Encoding. ASCII. getbytes (PARAM );
HttpwebrequestReq = (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(StreamReqstream = Req. getrequeststream ())
{
Reqstream. Write (BS, 0, BS. Length );
}
Using(WebresponseWR = 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.
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:
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 ()
{< br> // 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.
4. Use post to submit Chinese data. The post method completes data submission by entering parameters in the content of the page. Because the submitted parameters can indicate the encoding method used, more compatibility can be achieved theoretically. The code of the program that uses gb2312 encoding for access is as follows:
EncodingMyencoding =Encoding. Getencoding ("Gb2312");
StringParam =Httputility. Urlencode ("Parameter 1", Myencoding) +"="+Httputility. Urlencode ("Value 1", Myencoding) +"&"+Httputility. Urlencode ("Parameter 2", Myencoding) +"="+Httputility. Urlencode ("Value 2", Myencoding );
Byte[] Postbytes =Encoding. ASCII. getbytes (PARAM );
HttpwebrequestReq = (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(StreamReqstream = Req. getrequeststream ())
{
Reqstream. Write (BS, 0, BS. Length );
}
Using(WebresponseWR = Req. getresponse ())
{
// Process the received page content here
}
From the code above, we can see that when posting Chinese data, we first use the urlencode method to convert Chinese characters into encoded ASCII codes and then submit them to the server, the encoding method can be described during submission so that the recipient's server can correctly parse the code.
the above lists the interactions between client programs and servers using the HTTP protocol. Common get and post methods are used. The popular WebService also interacts with each other through the HTTP protocol, using the POST method. The difference is that the data content submitted by WebService and the received data content are all encoded in XML format. Therefore, httpwebrequest can also be used when WebService is called.