微信退款 The remote server returned an error: (400) Bad Request. 微信支付退款接口:基础连接已经关闭: 连接被意外关闭


微信退款接口需要添加证书, 特别注意:有些情况下即使在post请求时添加了证书依然报同样的错误,在添加证书时注意,

如果使用: X509Certificate cer = new X509Certificate(path, configInfo.mch_id); 也报错的话

改用:X509Certificate2 cer = new X509Certificate2(path, configInfo.mch_id);

private string requestWxRefundApi(WeixinPromotionChannelConfigInfo configInfo, string param)
{
  string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"bin\" + configInfo.cert_path;

  ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  X509Certificate2 cer = new X509Certificate2(path, configInfo.mch_id);
  HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(configInfo.refund_url);
  webrequest.ClientCertificates.Add(cer);
  webrequest.Method = "post";
  //webrequest.KeepAlive = true;
  //webrequest.Timeout = 60 * 1000;

  Encoding encoding = Encoding.UTF8;
  byte[] data = encoding.GetBytes(param);
  using (Stream inputStream = webrequest.GetRequestStream())
  {
    inputStream.Write(data, 0, data.Length);
  }

  string resp = string.Empty;
  using (HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse())
  {
    Stream stream = webreponse.GetResponseStream();
    using (StreamReader reader = new StreamReader(stream))
    {
      resp = reader.ReadToEnd();
    }
  }

  return resp;
}

  

————————————————
版权声明:本文为CSDN博主「Eric.chen1」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37693130/article/details/100563820


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM