1

过去我已经通过 HTTP 成功调用了 JSON Web 服务,但是现在我必须通过 HTTPS 进行 JSON POST。

我尝试使用适用于 HTTP 的代码,只是将被调用的 url 更改为 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.network.https> 无法保护 apitest.example.com 的现有连接。对 SSPI 的调用失败,请参阅内部异常。InnerException: System.ComponentModel.Win32Exception (0x80004005): 客户端和服务器无法通信,因为它们没有共同的算法

任何人都可以帮我解决通过 HTTPS 拨打电话需要做的事情吗?

4

1 回答 1

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);
            }
       }
于 2013-08-06T03:09:00.623 回答