C# 使用用户证书签名的HTTPS连接-不同计算机上的意外行为

C# 使用用户证书签名的HTTPS连接-不同计算机上的意外行为,c#,.net,https,certificate,x509,C#,.net,Https,Certificate,X509,有一项服务。我必须通过HTTPS协议连接到它,通过POST方法上传数据,然后下载结果。连接通过用户专用证书进行身份验证,证书格式为*.p12 我编写了以下函数来实现它: static public string PostLPDA(string fileName, string URL, string certificateFile) { X509Certificate cert = new X509Certificate(certificateFile); //

有一项服务。我必须通过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;
    }
静态公共字符串PostLPDA(字符串文件名、字符串URL、字符串证书文件)
{

X509Certificate cert=new X509Certificate(certificateFile);//从文件导入证书true);//忽略不受信任的站点错误OK,我找到了解决方案

证书必须在操作系统中注册。否则,它将无法工作


无论如何,谢谢您的关注。

certificateFile-它是否存在于其他计算机上?伊利亚:是的。当访问证书文件出现问题时,会出现错误