2017-03-18 85 views
1

我已经得到了与C#和一个简单的HTTPS请求一个问题...C#信息:System.Net.WebException在简单HTTPS请求

我想请求这个网址:https://api.sipgate.com/v1/在它工作正常一个网页浏览器,但我的C#-Code不工作:(有没有人一个想法,我做错了什么谢谢

using System.Net; 
using System.IO; 

// [...] 

WebRequest req = WebRequest.Create("https://api.sipgate.com/v1"); 
req.ContentType = "application/json"; 

try { 
    WebResponse res = req.GetResponse(); // Exception here... 
    Stream content = res.GetResponseStream(); 
    Console.WriteLine((new StreamReader(content)).ReadToEnd()); 
} catch (Exception e) { 
    Console.WriteLine(e.ToString());     
} 

这里是例外:?!

System.Net.WebException: Die zugrunde liegende Verbindung wurde geschlossen: Unerwarteter Fehler beim Senden.. ---> System.IO.IOException: Fehler bei Authentifizierung, da die Gegenseite den Transportstream geschlossen hat. 
    bei System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) 
    bei System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    bei System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    bei System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) 
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    bei System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) 
    bei System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
    bei System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
    bei System.Net.ConnectStream.WriteHeaders(Boolean async) 
    --- Ende der internen Ausnahmestapelüberwachung --- 
    bei System.Net.HttpWebRequest.GetResponse() 
    bei Sipgate_Test.Program.Main(String[] args) in c:\users\abc\documents\visual studio 2017\Projects\def\hij\Program.cs:Zeile 24. 

System.Net.WebException: 由于另一方关闭了传输流,因此身份验证错误。

回答

3

您缺少对SSL的支持。

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 

using (WebClient Client = new WebClient()) 
    string Content = Client.DownloadString("https://api.sipgate.com/v1"); 

编辑: 感谢您的指点在这@Crowcoder。在生产环境中,您不应该接受没有验证的任何证书。 ServerCertificateValidationCallback提供了一种访问您通常无法访问的SSL证书信息的方法。 例如,参数error会为您提供特定的证书错误,如名称不匹配。参数chain可以为您提供服务器发送的确切证书链,当系统存储缺少中间CA证书时,可用于构建证书链。

ServicePointManager.ServerCertificateValidationCallback += 
    (s, cert, chain, error) => 
{ 
    return error == SslPolicyErrors.None; 
}; 
+0

你不应该盲目地接受任何证书。 – Crowcoder

+0

谢谢,但这种新的代码引发相同的异常: ServicePointManager.ServerCertificateValidationCallback + =(S,证书,链,错误)=> { 返回错误== SslPolicyErrors.None; }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; (WebClient w = new WebClient()){ string ctn = w.DownloadString(“https://api.sipgate.com/v1”); Console.WriteLine(ctn); } – user3422533

+0

我上面更新了我的解决方案,它适用于我的本地机器。 :) –

0

您可以使用ServicePointManager检查证书并验证它是你所期望的一个,通过指纹或主题名称,例如。

但是,如果您信任该站点,则通常会在服务器上安装证书。

在Chrome开发工具安全选项卡中,您可以下载证书,然后将其安装在服务器上。您需要链接上的所有证书,如Certification Path选项卡上所示。

enter image description here