Csharp: HttpWebRequest and HttpWebResponse, httpwebresponse

Source: Internet
Author: User

Csharp: HttpWebRequest and HttpWebResponse, httpwebresponse

Http://stackoverflow.com/questions/4015324/http-request-with-post

 

Response. charset = "GBK"; // GBK // gb2312 this. codePage = 936; Session. codePage = 936; Encoding myEncoding = Encoding. getEncoding ("GBK"); byte [] data = System. text. encoding. getEncoding ("GBK "). getBytes ("907953"); // System. text. unicodeEncoding. ASCII. getBytes ("135246"); // UTF8 Base64Encoder myEncoder = new Base64Encoder (data); StringBuilder sb = new StringBuilder (); sb. append (myEncoder. getEncoded (); string content = HttpUtility. urlEncode ("juwen: three additional hours required: 2014-07-12: 15: 58, csharp", myEncoding); String sResult =" http://www2.lansea.com.cn/servlet/UserServiceAPI?method=sendSMS&isLongSms=0&username=87&password=232&smstype=0&mobile=13824350518&content= "+ Content +" "; // string param = HttpUtility. urlEncode (sResult, myEncoding); Response. write (sResult); byte [] postBytes = Encoding. getEncoding ("GBK "). getBytes (sResult); // Encoding. getEncoding ("GBK "). getBytes (sResult); try {HttpWebRequest req = (HttpWebRequest) HttpWebRequest. create (sResult); // sResult req. method = "POST"; req. contentType = "application/x-www-form-urlencoded; charset = GBK"; req. headers ["Accept-Charset"] = "GBK"; req. headers ["Accept-Language"] = "zh-CN, zh; q = 0. "; req. allowAutoRedirect = false; req. contentLength = postBytes. length; Stream webStream = req. getRequestStream (); // send data webStream. write (postBytes, 0, postBytes. length); webStream. close (); // get the returned data HttpWebResponse webResponse = (HttpWebResponse) req. getResponse (); StreamReader reader = new StreamReader (webResponse. getResponseStream (), Encoding. getEncoding ("GBK"); sResult = reader. readToEnd (); sResult. trim (); this. textBox1.Text = sResult; // returns whether the message is successfully sent.} catch (Exception ex) {ex. message. toString (); // sResult = ""; // return sResult ;}

 


C # HttpWebRequest continuous post

Hreq. Timeout = 2000;
The main reason for adding the above Code is that the default request timeout time is 100 seconds. when too many requests are opened and the main thread is blocked, what you said will happen.

I put my code in The onclick event of a Button. It can be normally displayed as 10. No problem. Didn't you leave the code in the Form constructor?

C # winform uses httpwebrequest and httpwebresponse for response. The response speed is sometimes slow. I don't know why

Httpwebrequest ----> http protocol

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.