我已经得到了与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: 由于另一方关闭了传输流,因此身份验证错误。
你不应该盲目地接受任何证书。 – Crowcoder
谢谢,但这种新的代码引发相同的异常: 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
我上面更新了我的解决方案,它适用于我的本地机器。 :) –