HttpWebRequest request error, the underlying connection has been closed: The connection was closed unexpectedly

Source: Internet
Author: User

Tag: Byte stream BSP sharp span SHA Exception part size

C # executes a POST request with HttpWebRequest "request error, underlying connection closed: Connection closed unexpectedly", Workaround:

System.Net.HttpWebRequest request;

Request. ProtocolVersion = Httpversion.version10;

---------------------------------------------------------------

Get method:

public string dowebrequest (string url) {HttpWebResponse WebResponse = null;            HttpWebRequest webRequest = (HttpWebRequest) webrequest.create (URL);            Webrequest.method = "POST";            string responsestr = null;            Webrequest.timeout = 50000; Webrequest.contenttype = "text/html;            charset=gb2312 ";             try {//attempt to get the return message of the URL to be requested WebResponse = (HttpWebResponse) webrequest.getresponse (); } catch (WebException e) {//A network error occurred when getting error response information RESPONSESTR = "A network error has occurred!"            Please try again later "; } catch (Exception e) {//The error message is returned as an error message when an exception occurs RESPONSESTR = "Error occurred:" + E .             Message;                    } finally {if (WebResponse! = null) {//Get network response stream using (StreamReader Responsereader = new StreamReader (webresponse.getresponsesTream (), encoding.getencoding ("GB2312"))) {responsestr = Responsereader.readtoe            nd ();//Get the contents of the return stream} webresponse.close ();//close the Web Response stream}}        return responsestr; }

Note: In the URL of the Chinese, you should first use httputility. UrlEncode ("Content", encoding) is encoded with the code received by the server.

Post method:

 private string Dowebrequestbypost (string url, string param) {HttpWebResponse WebResponse = null;            HttpWebRequest webRequest = (HttpWebRequest) webrequest.create (URL);            Use post to submit Webrequest.method = "POST";            string responsestr = null;            Webrequest.timeout = 50000; The byte array to post byte[] postbytes = encoding.                        GetBytes (param);            Webrequest.contenttype = "application/x-www-form-urlencoded;";             Webrequest.contentlength = Postbytes.length; using (Stream Reqstream = Webrequest.getrequeststream ()) {reqstream.write (postbytes, 0, Postbyt Es.            Length); try {//tries to get the return message of the URL to be requested WebResponse = (HttpWebResponse) webrequest.g            Etresponse ();            } catch (Exception) {//throw throw directly after error;   } finally {             if (WebResponse! = null) {//Get network response stream using (StreamReader                        Responsereader = new StreamReader (WebResponse.GetResponseStream (), encoding)) { Responsestr = Responsereader.readtoend ();//Get the contents of the return stream} webresponse.close ();//Turn off the Web ring        Should flow}} return responsestr; }

Encoding the encoding received for the server, for example: Encoding.GetEncoding ("GBK"), etc.
param POST Request Parameters param1=123&param2= Chinese &PARAM3=ABC Such a format, the Chinese part does not use the code, the method is converted into byte[] will be encoded.

HttpWebRequest request error, the underlying connection has been closed: The connection was closed unexpectedly

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.