Original: http://developer.51cto.com/art/200909/149995.htm
C # HttpWebRequest the way to submit data is actually get and post two, then the specific implementation and operational considerations is what? Then this article will give you a detailed description of the C # HttpWebRequest submission data methods of these two kinds of weapon. AD:2014WOT Global Software Technology Summit Beijing Station Course Video Release C # HttpWebRequest submit data method before we go to learn what is HttpWebRequest, which is a class in the. Net base Class library, under the namespace System.Net, Used to enable users to interact with the server through the HTTP protocol. The role of C # HttpWebRequest: HttpWebRequest The HTTP protocol is fully encapsulated, the HTTP protocol Header, Content, cookies have done the properties and methods of support, It's easy to write a program that simulates a browser's automatic login. C # HttpWebRequest Submit data way: 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: C # HttpWebRequest submit Data Mode 1. GET mode. The GET method completes the data submission by attaching parameters to the network address, such as the address http://Www.google.com/webhp?hl=zh-CN, the previous sectionhttp://www.google.com/webhprepresents the URL of the data submission, the following section HL=ZH-CN represents the additional parameters, where HL represents a key (key), 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 hereC # HttpWebRequest commits data Mode 2. POST mode. The POST method completes the data submission by filling in the parameters in the page content, and the format of the parameter is similar to the GET method, which is like HL=zh-cn&newwindow=1such a structure. The program code is as follows:stringparam ="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 herein 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. C # HttpWebRequest submit data Mode 3. Use the GET method to submit Chinese data. GET method to complete the data submission by attaching parameters in the network address, for Chinese encoding, commonly used gb2312 and UTF8 two kinds of code, GB2312 way to access the program codes as follows: Encoding myencoding= Encoding.GetEncoding ("gb2312"); stringAddress ="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 herein the program code above, we accessed the URL http in GET mode://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. C # HttpWebRequest submit data Mode 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 to access with the gb2312 method is as follows: Encoding myencoding= Encoding.GetEncoding ("gb2312"); stringparam =Httputility.urlencode ("parameter One", myencoding) +"="+ Httputility.urlencode ("Value One", myencoding) +"&"+ Httputility.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 (BS,0, BS. Length); } using(WebResponse WR =req. GetResponse ()) {//handle the content of the received page hereAs can be seen from the above code, the post Chinese data, the first time using the UrlEncode method to convert the characters into 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. The basics of how C # HttpWebRequest submits data are introduced here, and hopefully it helps to understand and learn how C # HttpWebRequest submits data.
Analysis of the way of submitting data in C # HttpWebRequest