Use post [send data] And webclientpost in WebClient
In many cases, we need to use the WebClient in C # to send and receive data. The WebClient class provides public methods to send data to any local, Intranet, or Internet Resource identified by the URI and to receive data from these resources.
The following describes the main functions of WebClient.
WebClientConstructor
The. Ctor includes an empty constructor and a static constructor. The static constructor mainly serves as the initialization function of reference byte [] data for UrlEncode and UrlEncodeAndWirte encoding.
Stati WebClient ()
Public WebClient ()
WebClientProvides four methods to upload data to a resource:
- OpenWrite returns a Stream used to send data to resources.
- UploadData sends the byte array to the resource and returns the byte array containing any response.
- UploadFile sends the local file to the resource and returns a byte array containing any response.
- UploadValues sends the NameValueCollection to the resource and returns a byte array containing any response.
WebClientThere are also three methods to download data from a resource:
- DownloadData downloads data from the resource and returns a byte array.
- DownloadFile downloads data from a resource to a local file.
- OpenRead returns data from a resource in the form of Stream.
After learning about WebClient, we started to get started.
Sending data through Post can avoid the limit on the length of data in Get mode. The following uses WebClient to implement this function. The Web server can be any CGI, but you need to understand the encoding accepted by the Web end. The Code is as follows:
WebClient wc = new WebClient (); StringBuilder postData = new StringBuilder (); postData. append ("formField1 =" + "form data 1"); postData. append ("& formField2 =" + "form data 2"); postData. append ("& formField3 =" + "form data 3"); // The following is GB2312 encoded byte [] sendData = Encoding. getEncoding ("GB2312 "). getBytes (postData. toString (); wc. headers. add ("Content-Type", "application/x-www-form-urlencoded"); wc. headers. add ("ContentLength", sendData. length. toString (); byte [] recData = wc. uploadData ("http://www.domain.cn/services/DataImport1.asp", "POST", sendData); // display the returned value with the encoded MessageBox. show (Encoding. getEncoding ("GB2312 "). getString (recData ));
Note that "form data x" must be used when "&", "=", "+,
HttpUtility. UrlEncode ("++ xxx why not encode", Encoding. GetEncoding ("GB2312") Encoding
HttpUtility. UrlEncode (string) uses UTF-8 encoding by default, so when UrlEncode encoding is used and the field contains Chinese characters, and when the target website uses GB2312, You need to specify in the UrlEncode function that uses Gb2312
The preceding code can be modified as follows:
PostData. Append ("formField1 =" + HttpUtility. UrlEncode ("form data 1", Encoding. GetEncoding ("GB2312 ")));
PostData. Append ("& formField2 =" + HttpUtility. UrlEncode ("form data 2", Encoding. GetEncoding ("GB2312 ")));
Webclient wants to send post data via QQ ()
C #: How to submit data to another webpage in the background
You can use WebClient.
Use UploadString of WebClient (URL, "POST", Post data processed)