有服务。我必须通过 HTTPS 协议连接到它,通过 POST 方法上传数据,然后下载结果。连接使用 *.p12 格式的用户私有证书进行身份验证。
我编写了以下功能来实现它:
static public string PostLPDA(string fileName, string URL, string certificateFile)
{
X509Certificate cert = new X509Certificate(certificateFile); // importing certificate from file <===== 1
StreamReader sr = new StreamReader(fileName);
string postString = string.Format("Query={0}", sr.ReadToEnd()); // preparing data to send
const string contentType = "application/x-www-form-urlencoded";
System.Net.ServicePointManager.Expect100Continue = false;
CookieContainer cookies = new CookieContainer();
HttpWebRequest webRequest = WebRequest.Create(URL) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = contentType;
webRequest.CookieContainer = cookies;
webRequest.ContentLength = postString.Length;
webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webRequest.ClientCertificates.Add(cert); //adding certificate
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, certificate, chain, sslPolicyErrors) => true); // ignore untrusted site error <===== 2
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream()); // <===== 3
requestWriter.Write(postString);
requestWriter.Close();
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
webRequest.GetResponse().Close();
return responseData;
}
在我的电脑上一切正常 - 没有异常、错误、数据成功上传、服务有适当的响应。
但是在每台其他 PC 上,标有 <====== 3 的行都有异常,并带有消息:
请求被中止:无法创建 SSL/TLS 安全通道。
我正在使用相同的证书文件、相同的地址和相同的数据。结果是错误的,我不知道出了什么问题。
有人可以帮忙吗?