//请求api 带证书的方法
/// </summary>
/// <param name="URL"></param>
/// <param name="strPostdata"></param>
/// <param name="strEncoding"></param>
/// <returns></returns>
public bool CheckValidationResult(
Object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
// Always accept
return true;
}
public string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
{
try
{
Encoding encoding = Encoding.UTF8;
string cert = @"C:\zhengshu\apiclient_cert.p12";
string password = "xxxxxxxxxx";//这里是填写的是 微信支付商户号
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
X509Certificate cer = new X509Certificate(cert, password);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.ClientCertificates.Add(cer);
request.Method = "post";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/x-www-form-urlencoded";
byte[] buffer = encoding.GetBytes(strPostdata);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
{
return reader.ReadToEnd();
}
}
catch (Exception e)
{
return "";
}
}
//调用OpenReadWithHttps方法 其中sendxml已经构造好了没有问题
Class1 ojb = new Class1();
ojb.OpenReadWithHttps(url, sendxml, "UTF-8");
现在就是网页没有认为反馈,按道理腾讯是不是应该回馈一个xml?我这里哪里又问题,请大神帮忙给看看。调用证书这块有没有问题,我将证书C:\zhengshu\apiclient_cert.p12放到这里目录里并安装了。