皮蛋丿真皮 2022-10-13 15:26 采纳率: 100%
浏览 190
已结题

HTTP POST:基础连接 : 连接被意外关闭

HttpPost 报错 System.Net.WebException: 基础连接 : 连接被意外关闭

调用失败:System.Net.WebException: 基础连接已经关闭: 连接被意外关闭。
在 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
在 System.Net.HttpWebRequest.GetRequestStream()
在 WCS.Common.Util.CSHttpHelp.HttpPost(String postUrl, String postBody)
在 Client.Call(String action, String floor, Int32 type)

postman可以正常访问,各种方法都尝试了,都不行
代码如下:
public static string HttpPost(string postUrl, string postBody)
          {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(postUrl);
            request.ContentType = "application/json";
            request.Method = "POST";
            request.Timeout = 300000;
            request.KeepAlive = false;
            request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(postBody);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);

            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            using (System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return myreader.ReadToEnd();
            }
        }

  • 写回答

3条回答 默认 最新

  • Jackyin0720 2022-10-13 15:56
    关注

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    你在WebRequest 请求之前加上这2句试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月21日
  • 已采纳回答 10月13日
  • 赞助了问题酬金15元 10月13日
  • 赞助了问题酬金1元 10月13日
  • 展开全部

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果