二次封装短信接口,在https加载证书上遇到问题--急救-在线等
服务商给我们提供一个https接口,我计划在cs代码用HttpWebRequest方法post数据到服务提供商的接口里,接口提供两个证书:client.p12(个人的),根(root.p12)
下面是我的函数,去发送短信内容,但是找不到证书,错在什么地方
public static void SentMessage(int MessageID,string PhoneNum,long ID,long UserID)
{
string mobile_no=PhoneNum;
string msg=GetMessage(MessageID,UserID);
string send_time=DateTime.Now.ToShortDateString();
string site_id= " ";
string sign= " ";
string link_id= " ";
string charset= " ";
string mo_id= " ";
string postData = "mobile_no= "+mobile_no+ "&msg= "+msg+ "&send_time= "+send_time+ "&site_id= "+site_id+ "&sign= "+sign+ "&link_id= "+link_id+ "&charset= "+charset+ "&msg= "+mo_id+ "&msg= "+mo_id;
Encoding encoding = Encoding.GetEncoding( "gb2312 ");
string strUrl = "https://219.133.40.247/zhyc/sendmsg.jsp ";
byte[] data = encoding.GetBytes(postData);
//准备请求...
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST ";
myRequest.ContentType = "application/x-www-form-urlencoded ";
myRequest.ContentLength = data.Length;
//加载证书
// WSE 2.0 method
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore );
store.OpenRead();
//读取证书的keyid
Microsoft.Web.Services2.Security.X509.X509CertificateCollection certs =
store.FindCertificateBySubjectName( "CN=Eddy,E=eddywu@tencent.com ");
X509SecurityToken token = null;
try
{
// This sample obtains the first matching certificate from the collection.
token = new X509SecurityToken( ((Microsoft.Web.Services2.Security.X509.X509Certificate) certs[0]) );
}
catch(Exception ex)
{
throw new Exception(ex.ToString());
}
myRequest.KeepAlive = true;
myRequest.ClientCertificates.Add(token.Certificate);
Stream newStream = myRequest.GetRequestStream();
//发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();
//读取回来的数据
HttpWebResponse res = (HttpWebResponse)myRequest.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();
}
------解决方案--------------------up
------解决方案--------------------值得关注
学习中。。。。
------解决方案--------------------可能是在设置证书的时候的有问题
------解决方案--------------------帮顶
------解决方案--------------------