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¶m2= 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