过去,我已成功通过HTTP调用JSON Web服务 但是,现在我必须通过HTTPS进行JSON POST。
我尝试使用适用于HTTP的代码,只是将被调用的网址更改为https但不起作用。
这是我正在使用的代码...
WebRequest wrGETURL;
wrGETURL = WebRequest.Create("https://apitest.example.com/geo/coverage/v1/?appId=2644571&appKey=836621d715b6ce4db5f007d8fa2214f");
wrGETURL.Method = "POST";
Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string responseFromServer = objReader.ReadToEnd();
我在fiddler中看到的错误信息是:
fiddler.network.https>无法保护apitest.example.com的现有连接。对SSPI的调用失败,请参阅内部异常。 InnerException:System.ComponentModel.Win32Exception(0x80004005):客户端和服务器无法通信,因为它们没有通用算法
任何人都可以帮助我通过HTTPS拨打电话吗?
答案 0 :(得分:1)
您是否需要对服务器证书进行身份验证或回调? 在大多数情况下,这对我有用:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://someurl/");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
// Create NetworkCredential Object
NetworkCredential admin_auth = new NetworkCredential("username", "password");
// Set your HTTP credentials in your request header
httpWebRequest.Credentials = admin_auth;
// callback for handling server certificates
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"name\":\"TEST_123\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
}