无法从传输读取数据 connection:An 现有连接被远程主机强行关闭
Unable to read data from the transport connection:An existing connection was forcibly closed by the remote host
我尝试从 morizon.pl 下载源页面时遇到问题:
WebClient webClient = new WebClient();
try
{
string str = webClient.DownloadString("https://www.morizon.pl/");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
我在 Whosebug 中检查了类似的问题并编辑了我的代码,但仍然没有,我被这个问题困住了。
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
castRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36";
castRequest.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
castRequest.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
castRequest.KeepAlive = false;
castRequest.ProtocolVersion = HttpVersion.Version10;
castRequest.ServicePoint.ConnectionLimit = 1;
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
例如google.com我可以用我的功能下载但是morizon.pl我不能。
自 .NET Framework 4.0 起,默认安全协议为 TLS 1.0
和 SSL 3.0
。
在您的应用程序中,您可能需要启用 TLS 1.1
and/or TLS 1.2
.
System.Net.ServicePointManager.SecurityProtocol |=
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using (WebClient webClient = new WebClient())
{
string str = webClient.DownloadString("https://www.morizon.pl/");
}
更多详细信息在此 。
我尝试从 morizon.pl 下载源页面时遇到问题:
WebClient webClient = new WebClient();
try
{
string str = webClient.DownloadString("https://www.morizon.pl/");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
我在 Whosebug 中检查了类似的问题并编辑了我的代码,但仍然没有,我被这个问题困住了。
public class CookieAwareWebClient : WebClient
{
public CookieContainer CookieContainer { get; set; }
public CookieAwareWebClient()
: this(new CookieContainer())
{ }
public CookieAwareWebClient(CookieContainer c)
{
this.CookieContainer = c;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
var castRequest = request as HttpWebRequest;
if (castRequest != null)
{
castRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
castRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36";
castRequest.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
castRequest.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
castRequest.KeepAlive = false;
castRequest.ProtocolVersion = HttpVersion.Version10;
castRequest.ServicePoint.ConnectionLimit = 1;
castRequest.CookieContainer = this.CookieContainer;
}
return request;
}
}
例如google.com我可以用我的功能下载但是morizon.pl我不能。
自 .NET Framework 4.0 起,默认安全协议为 TLS 1.0
和 SSL 3.0
。
在您的应用程序中,您可能需要启用 TLS 1.1
and/or TLS 1.2
.
System.Net.ServicePointManager.SecurityProtocol |=
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using (WebClient webClient = new WebClient())
{
string str = webClient.DownloadString("https://www.morizon.pl/");
}
更多详细信息在此