微信小微商户证书和签名篇 (2)

获取证书序列号

string url = "https://api.mch.weixin.qq.com/risk/getcertficates"; WxPayDataToXiaoWei inputObj = new WxPayDataToXiaoWei(); inputObj.SetValue("mch_id", weixinConfig.MchID); inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); inputObj.SetValue("sign_type", "HMAC-SHA256"); inputObj.SetValue("sign", inputObj.MakeSign(weixinConfig.Key)); var respones = WeixinXiaoweiService.Post(inputObj.ToXml(), url, false, true, 1000); try { //获取证书序列号 inputObj = new WxPayDataToXiaoWei(); inputObj.FromXml(respones); if (inputObj.GetValue("return_code").ToString() == "SUCCESS" && inputObj.GetValue("result_code").ToString() == "SUCCESS") { var certificates = inputObj.GetValue("certificates").ToString();//获取证书信息 var data = JsonConvert.DeserializeObject<dynamic>(certificates); var obj = JsonConvert.DeserializeObject<List<dynamic>>(data.data.ToString()); weixinConfig.serial_no = obj[0].serial_no.ToString();//证书序列号 weixinConfig.encrypt_certificate = JsonConvert.DeserializeObject<encrypt_certificate>(obj[0].encrypt_certificate.ToString()); } return ProtocolManager.GetPackage(PackageReturnCode.OK, "获取成功!"); } catch (Exception ex) { LogHelper.Error("获取证书失败:" + ex.ToString()); return ProtocolManager.GetPackage(PackageReturnCode.Fail, "获取失败!"); }

说明: sign签名方法是 inputObj.MakeSign(weixinConfig.Key),//注:key为商户平台设置的密钥key; WeixinXiaoweiService.Post(inputObj.ToXml(), url, false, true, 1000); 这个方法是下载的微信官方Demo中的HttpService类中,下面是修改的

public static string Post(string body, string url, bool isUseCert,bool isxml, int timeout) { string result = "";//返回结果 HttpWebRequest request = null; HttpWebResponse response = null; Stream reqStream = null; try { //设置最大连接数 ServicePointManager.DefaultConnectionLimit = 200; //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } /*************************************************************** * 下面设置HttpWebRequest的相关属性 * ************************************************************/ request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = USER_AGENT; request.Method = "POST"; request.Timeout = timeout * 1000; //设置代理服务器 //WebProxy proxy = new WebProxy(); //定义一个网关对象 //proxy.Address = new Uri(WxPayConfig.PROXY_URL); //网关服务器端口:端口 //request.Proxy = proxy; //设置POST的数据类型和长度 request.ContentType = isxml? "text/xml": "application/json"; byte[] data = System.Text.Encoding.UTF8.GetBytes(body); request.ContentLength = data.Length; //是否使用证书 if (isUseCert) { // X509Certificate2 cert = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"WxXiaoWeiCert\apiclient_cert.p12"), weixinConfig.SSlCertPassword); X509Certificate2 cert = new X509Certificate2(weixinConfig.SSlCertPath,weixinConfig.SSlCertPassword); request.ClientCertificates.Add(cert); LogHelper .Debug("WxPayApi", "PostXml used cert"); } //往服务器写入数据 reqStream = request.GetRequestStream(); reqStream.Write(data, 0, data.Length); reqStream.Close(); //获取服务端返回 response = (HttpWebResponse)request.GetResponse(); //获取服务端返回数据 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (System.Threading.ThreadAbortException e) { LogHelper.Error("HttpService", "Thread - caught ThreadAbortException - resetting."); LogHelper.Error("Exception message: {0}", e.Message); System.Threading.Thread.ResetAbort(); } catch (WebException e) { LogHelper.Error("HttpService", e.ToString()); if (e.Status == WebExceptionStatus.ProtocolError) { LogHelper.Error("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode); LogHelper.Error("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription); } throw new Exception(e.ToString()); } catch (Exception e) { LogHelper.Error("HttpService", e.ToString()); throw new Exception(e.ToString()); } finally { //关闭连接和流 if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return result; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsffy.html