System.Net.WebException:底层连接已关闭:接收时发生意外错误
我正在尝试在 C# 中创建一个方法来从 url 返回网页 html 内容的字符串。 我尝试了几种不同的方法,但收到错误System.Net.WebException:底层连接已关闭:接收时发生意外错误。
以下内容在本地工作正常,但得到上述内容在远程服务器上运行时出错:
public static string WebPageRead(string url)
{
string result = String.Empty;
WebResponse response = null;
StreamReader reader = null;
try
{
if (!String.IsNullOrEmpty(url))
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
response = request.GetResponse();
reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
}
return result;
}
I'm trying to create a method in C# to return a string of a web pages html content from the url. I have tried several different ways, but I am getting the error System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.
The following works fine locally, but gets the above error when running on a remote server:
public static string WebPageRead(string url)
{
string result = String.Empty;
WebResponse response = null;
StreamReader reader = null;
try
{
if (!String.IsNullOrEmpty(url))
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
response = request.GetResponse();
reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = reader.ReadToEnd();
}
}
catch (Exception exc)
{
throw exc;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能不是问题所在,但请尝试以下操作:
我回显了之前的答案,建议您使用已知的良好 URL 进行尝试。 我要补充的是,您应该使用已知良好的 HTTP 1.1 URL 来尝试此操作,注释掉将版本设置为 1.0 的行。 如果这有效,那么它就会大大缩小范围。
This is probably not the problem, but try the following:
I echo the earlier answer that suggests you try this with a known good URL. I'll add that you should try this with a known good HTTP 1.1 URL, commenting out the line that sets the version to 1.0. If that works, then it narrows things down considerably.
感谢您的回复,问题是由于远程服务器上的 DNS 问题造成的! 为了确认一下,我最后使用了以下代码:
Thanks for the responses, the problem was due to a DNS issue on the remote server! Just to confirm, I went with the following code in the end:
之前也遇到过这样的问题,通过在有问题的机器上的 IE 中打开 url 来解决。 然后 IE 会询问您是否要将 URL 添加到安全站点列表中。 添加它,它适用于该 url。
这只是可能的原因之一。 严重的是,很多其他问题都可能导致此问题。 除了上述问题之外,我发现解决此问题的最佳方法是捕获异常并重试请求。
Had a problem like this before that was solved by opening the url in IE on the machine with the problem. IE then asks you whether you want to add the url to the list of secure sites. Add it and it works for that url.
This is just one of the possible causes. Seriously a lot of other problems could cause this. Besides the problem described above, the best way I've found to solve this is the just catch the exception and retry the request.