缺少C#私钥 - 证书安装时带有私钥

时间:2015-02-24 12:03:54

标签: c# asp.net

我编写代码来访问另一台服务器上的服务

证书是在我的服务器上使用私钥安装的

当我运行测试时

if (cer.HasPrivateKey)
            {
                Response.Write("OK");

            }
            else
            {
                Response.Write("Nooo");
            }

我好了=所以证书有一个私钥

但是当我运行整个代码时

 try
        {


            //  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpWebRequest httpWReq =
                 (HttpWebRequest)WebRequest.Create("https://gw.bisan.com/api/apdemo_6");

            Encoding encoding = new UTF8Encoding();
            string postData = @"{""user"":""cube"",""password"":""258970"",""command"":""table"",""table"":""currency"",""filters"":[{""field"":""code"",""operation"":""!\u003d"",""value"":""01""}],""fields"":[""symbol"",""rate""]}";
            byte[] data = encoding.GetBytes(postData);

            httpWReq.ProtocolVersion = HttpVersion.Version11;
            httpWReq.Method = "POST";
            httpWReq.ContentType = "application/json";//charset=UTF-8";


            var certificate = @"\www.cube-its.com.pfx";
            X509Certificate2 cer = new X509Certificate2(new X509Certificate2(Server.MapPath(certificate), "XXXX"));

            httpWReq.ClientCertificates.Add(cer);

            Response.Write(cer.PrivateKey.ToString());
            if (cer.HasPrivateKey)
            {
                Response.Write("OK");

            }
            else
            {
                Response.Write("Nooo");
            }

            httpWReq.ContentLength = data.Length;



            Stream stream = httpWReq.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();

            HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();

            string s = response.ToString();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            String jsonresponse = "";
            String temp = null;
            while ((temp = reader.ReadLine()) != null)
            {
                jsonresponse += temp;
            }

            Response.Write("Json Response : " + jsonresponse);

        }
        catch (WebException ex1)
        {
            { Response.Write("Exception 2 : " + ex1.Message); }
        }
        catch (Exception ex) { Response.Write("Exception : " + ex.Message); }

我总是遇到服务器错误400错误请求

当我向公司表示他们说该请求缺少私钥时

那我该怎么做'thanx

0 个答案:

没有答案