当我调用这个API时,我通过jQuery获得结果,而不是通过c#
本文关键字:结果 jQuery API 调用 | 更新日期: 2023-09-27 18:31:24
当我调用这个 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;
尝试在请求中启用cookie,某些api要求它(PayPal例如):
CookieContainer cookieContainer = new CookieContainer();
yourWebRequest.CookieContainer = cookieContainer;
您收到错误invalid certificate
,因为您的请求正在构建 SSL 连接,但实际上您连接到非 SSL 端口 80。服务器返回一个通常的html页面,因为HttpWebRequest正在等待SSL握手。这给了证书错误。
使用 SSL 时,请使用 UriBuilder 构建指向配置了 SSL 的服务器端口的 Uri
。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());