如题,在使用统一下单接口时,提交数据时报,基础连接已经关闭: 发送时发生错误。这种情况是什么原因造成的呢?
代码如下:
System.GC.Collect();//垃圾回收
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var data = Encoding.GetEncoding("UTF-8").GetBytes(postStr);
Stream responseStream;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ServicePoint.ConnectionLimit = 300;
if (request == null)
{
throw new ApplicationException(string.Format("Invalid url string: {0}", url));
}
// request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();//报错点
requestStream.Write(data, 0, data.Length);
requestStream.Close();
try
{
responseStream = request.GetResponse().GetResponseStream();
}
catch (Exception exception)
{
throw exception;
}
string str = string.Empty;
using (StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8")))
{
str = reader.ReadToEnd();
}
responseStream.Close();
结贴
把request.ContentLength = data.Length;去掉即可
后又遇到response = (HttpWebResponse)request.GetResponse();报相同的错
网上各种方法试了个变,反正就是我服务器的事,没有用代理。
最后是服务器访问限制了,导致访问不了接口地址:https://api.mch.weixin.qq.com/pay/unifiedorder
https://www.cnblogs.com/farb/p/HttpRequestProblem.html ,可以参考一下这里。这是你本身请求上的问题。