微信官方退款接口文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
参数按照文档构造 记录出现错误:System.Net.WebException: The remote server returned an error: (400) Bad Request
这个是由于退款接口需要传证书
证书的话是需要登录商户号进行下载api证书 下载好导入到服务器中 调用支付接口的时候添加证书即可
public string PostWebRequest(string postUrl, string paramData) { string ret = string.Empty; try {
byte[] byteArray = Encoding.Default.GetBytes(paramData); //转化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
//初始化证书 fileName:证书存放的地址 password 证书的密码 X509Certificate2 certificate = new X509Certificate2(fileName,password);
//添加证书 webReq.ClientCertificates.Add(certificate);
webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); // newStream.Write(byteArray, 0, byteArray.Length);//写入参数 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); //这里报错 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); newStream.Close(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { return ex.Message; } return ret; }
标签:byteArray,string,退款,微信,jsapi,证书,newStream,Close,webReq From: https://www.cnblogs.com/zzhsmallblog/p/18522905