我的应用程序托管在云上(Microsoft Azure),我试图使用https://api.nexmo.com/ni/basic/json?api_key=xxx&api_secret=xxxxxx&number=验证手机号码,一切都正常工作,然后突然我现在不再能够通过Azure连接到api ,虽然我可以在本地连接到api,但是却出现此错误远程主机强行关闭了现有连接。我尝试将TLS更新到V1.2,但始终收到相同的错误。
这是我的代码:
string nexmoRes = Utilities.ReadGetHtmlPage("https://api.nexmo.com/ni/basic/json?api_key=xxxx&api_secret=xxx&number=" + InternationalMSISDN, "");
public static string ReadGetHtmlPage(string url, string str)
{
try
{
try
{
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
Utilities.writeLogs(System.Reflection.MethodInfo.GetCurrentMethod(), "url " + url);
objRequest.Method = "GET";
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
StreamReader sr;
sr = new StreamReader(objResponse.GetResponseStream());
return sr.ReadToEnd();
}
catch (Exception ex)
{
Utilities.WriteToText(ex);
return "";
}
}
catch (Exception ex)
{
Utilities.WriteToText(ex);
return "";
}
}
答案 0 :(得分:2)
在App Service上进行了尝试,对我来说效果很好,请确保您定位到.NET 4.7 in your web.config或在ServicePointManager
中明确选择TLS 1.2,因为您的遥控器仅讲TLS 1.2 —
$ curl -i --tlsv1.0 "https://api.nexmo.com/ni/basic/json"
curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed
$ curl -i --tlsv1.1 "https://api.nexmo.com/ni/basic/json"
curl: (35) schannel: failed to receive handshake, SSL/TLS connection failed
$ curl -i --tlsv1.2 "https://api.nexmo.com/ni/basic/json"
HTTP/1.1 200 OK
Server: nginx
...
{
"status": 4,
"status_message": "Invalid credentials"
}
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
...
如果您走这条路,请注意ServicePointManager.SecurityProtocol
的范围是您的整个应用程序域。
定位.NET 4.7而不是修补ServicePointManager
会带您到这里,这是个更好的地方-
带有Windows worker的应用程序服务在Windows Server 2016上运行-