簡體   English   中英

簡單HTTPS請求中的System.Net.WebException

[英]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;
};

您可以使用ServicePointManager檢查證書,並通過指紋或主題名稱驗證它是您所期望的證書。

但是,如果您信任該站點,則通常會在服務器上安裝證書。

在Chrome開發工具的安全選項卡中,您可以下載證書,然后將其安裝在服務器上。 您將需要鏈中的所有證書,如“ Certification Path選項卡上所示。

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM