服务商给我们提供一个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,[email protected]");
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();  }

解决方案 »

  1.   

    没做过这种接口的,只自己写了一个windows服务,定时每隔2秒,从数据库读出来发送。web程序只管把短信插入到发送数据库队列就不用管了
      

  2.   

    不知道证书的事情My-Blog地址1:http://www.cnblogs.com/nyzfl
    My-Blog地址2: http://blog.csdn.net/nyzfl
      

  3.   

    to:junzhang4008(程序可以让尸体动起来)
    就是做成软件,也需要post数据,同样也需要身份验证的
      

  4.   

    这是我的,给你参考一下System.Net.WebClient client = new System.Net.WebClient();
    client.Headers.Add(参数自己写);
    byte[] bs = client.DownloadData(Myurl); //Myurl 为结果返回路径
    string url = "https://219.133.40.247/zhyc/sendmsg.jsp?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;
    byte[] bas = client.DownloadData(url) 
    //发送信息,返回值没有什么意思的,只要能触发这个发送事件就好
      

  5.   

    client.DownloadData(url),用这个不能使用证书吧??
    WebClient!!