C# 使用SSL访问webservice(自用)

来源:互联网 发布:中国不成熟的网络环境 编辑:程序博客网 时间:2024/06/10 23:42

{

ServicePointManager.ServerCertificateValidationCallback= new RemoteCertificateValidationCallback((a, b, c, d) => { return true; });//表示不对服务端证书进行有效性校验

SSLWebService sws = new SSLWebService();

ws.ClientCertificates.Add(X509Certificate.CreateFromCertFile(Path.GetFullPath(@"../../") + @"Resourcescerfile.cer"));

sws.DoSomeThing(“Hello World!”);

}

{

string reqUrl = "";

/// 构建请求的HttpWebRequest对象

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUrl);
       req.KeepAlive = true;

/// 从本地文件中加载证书
       req.ClientCertificates.Add(X509Certificate.CreateFromCertFile(ConfigurationManager.AppSettings["CertFilePath"]));
       string requestValue = "<request><ver>版本0.1</ver></request>";
       byte[] requestData = Encoding.UTF8.GetBytes(requestValue);

req.Method = "POST";
       req.ContentType = "application/x-www-form-urlencoded;charset=UTF8";
       req.ContentLength = requestData.Length;


}

原创粉丝点击