Use post [send data] And webclientpost in WebClient

Source: Internet
Author: User

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)

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.