/// <summary> /// 发送POST请求 /// </summary> /// <param name="json"></param> /// <returns></returns> public static string HttpPost(string requestBody) { string responseMsg = ""; try { HttpWebRequest request; //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; request = WebRequest.Create(url) as HttpWebRequest; } else { request = WebRequest.Create(url) as HttpWebRequest; } request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; request.Timeout = HttpTimeOut; request.ReadWriteTimeout = HttpTimeOut; byte[] data = Encoding.Default.GetBytes(requestBody); using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()); responseMsg = sr.ReadToEnd(); } catch (Exception ex) { LogHelper.WriteLog("HttpPost", ex); } return responseMsg; }
最近用let‘s encrypt制作里的https证书,给网站改为Https.更改后之前做的一些http post请求报错,提示System.Net.WebException: 基础连接已经关闭: 发送时发生错误。 ---> System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节。Received an unexpected EOF or 0 bytes from the transport stream
最终在国外某网站找到解决方法,原来是自己的接口程序.net framework版本太低。改为.net 4.5以上版本即可。
原文地址:https://www.cnblogs.com/xbzhu/p/9120167.html
时间: 2024-10-11 15:02:18