注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

还东国的博客

行之苟有恒,久久自芬芳

 
 
 

日志

 
 

(转载)微信红包CA证书出错  

2015-02-11 18:50:39|  分类: NET(C#) |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

(转载)微信红包CA证书出错

http://blog.csdn.net/tianzhixiaoyu/article/details/43699861#userconsent#

我们在对接微信商户平台的现金红包高级接口时向

https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack

接口地址post数据后 返回的xml文件中报 CA证书出错

 

详细返回XML包:

<xml>

<return_code><![CDATA[FAIL]]></return_code>

<return_msg><![CDATA[CA证书出错.]]></return_msg>

<result_code><![CDATA[FAIL]]></result_code>

<err_code><![CDATA[NOAUTH]]></err_code>

<err_code_des><![CDATA[CA证书出错.]]></err_code_des>

<mch_billno><![CDATA[10017876201412311755277529]]></mch_billno>

<mch_id>10017876</mch_id>

<wxappid><![CDATA[wx83ebd841d284292b]]></wxappid>

<re_openid><![CDATA[osrd5uFQmotuVVq4lnvRh3O4x748]]></re_openid>

<total_amount>100</total_amount>

</xml>

 

原因是向微信服务器发送请求数据时没有带证书请求:

 

    /// <summary>

    /// 发送请求

    /// </summary>

    /// <param name="data">发送拼接的参数</param>

    /// <param name="url">要发送到的链接地址</param>

    /// <returns>返回xml</returns>

 

        public static string Send(string data, string url)

        {

            return Send(Encoding.GetEncoding("UTF-8").GetBytes(data), url);

        }

 

        public static string Send(byte[] data, string url)

        {

            string cert = ConfigurationManager.AppSettings["CertPath"].Trim();//证书存放的地址

            string password = ConfigurationManager.AppSettings["MchId"].Trim();//证书密码 即商户号

            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

            X509Certificate cer = new X509Certificate(cert, password);

 

            #region 该部分是关键,若没有该部分则在IIS下会报 CA证书出错

            X509Certificate2 certificate = new X509Certificate2(cert, password);

            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadWrite);

            store.Remove(certificate);   //可省略

            store.Add(certificate);

            store.Close();

 

            #endregion

 

 

            Stream responseStream;

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            if (request == null)

            {

                throw new ApplicationException(string.Format("Invalid url string: {0}", url));

            }

            request.UserAgent = sUserAgent;

            request.ContentType = sContentType;

            request.ClientCertificates.Add(cer);

 

 

            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();

            return str;

        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

        {

            if (errors == SslPolicyErrors.None)

                return true;

            return false;

        }

    }

  评论这张
 
阅读(4488)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017