[英]Handle Unhandled Exception: System.Net.WebException: The request timed out?
[英]System.Net.WebException in simple HTTPS Request
我遇到了C#和簡單的HTTPS-Request問題......
我想請求這個URL: https ://api.sipgate.com/v1/在webbrowser中,它工作正常,但我的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:
身份驗證出錯,因為另一方已關閉傳輸流。
您缺少對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;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.