0

当我调用这个 API 时,我会得到invalid certificate.

如何解决这个问题?

 string res = string.Empty;
 string str = context.Request["params"].ToString();

 string json = new JavaScriptSerializer().Serialize(new
 {
   login = "aaa",
   password = "ssss",
   command = "ssl_decoder",
   ssl_certificate = str
 });

 var httpWebRequest = (HttpWebRequest)WebRequest
    .Create("https://api.sslguru.com?params="+str.Normalize());

 httpWebRequest.ContentType = "text/json";
 httpWebRequest.Method = "POST";

 using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
 {
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
      res = streamReader.ReadToEnd();
    }
 }

 return res;
4

2 回答 2

0

您收到错误invalid certificate是因为您的请求正在建立 SSL 连接,但您实际上连接到非 SSL 端口 80。如果 HttpWebRequest 正在等待 SSL 握手,服务器会返回一个普通的 html 页面。这会导致证书错误。

当您使用 SSL 时,请通过使用UriBuilder来构建您的 Uri 以指向已配置 SSL 的服务器端口。

var httpWebRequest = (HttpWebRequest)WebRequest
    .Create(new UriBuilder("https", 
                           "api.sslguru.com", 
                            443, /* THE PORT THAT IS CONFIGURED FOR TLS/SSL */
                            "", 
                            "?params="+str.Normalize()).Uri);

或者,如果您更喜欢快速修复类型的开发人员:

var httpWebRequest = (HttpWebRequest)WebRequest
    .Create("https://api.sslguru.com:443?params="+str.Normalize());
于 2014-02-08T21:09:28.017 回答
0

尝试在请求中启用 cookie,一些 api 要求它(说贝宝):

CookieContainer cookieContainer = new CookieContainer();
yourWebRequest.CookieContainer = cookieContainer;
于 2013-09-16T11:17:08.417 回答