c# - "A connection attempt failed because the connected party did not properly respond after a period of time"使用 WebClient

标签 c# sockets webclient

我正在使用以下在本地机器上运行的代码,但是当我在服务器上尝试相同的代码时它会抛出错误

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

这是我的代码:

WebClient client = new WebClient();
// Add a user agent header in case the 
// requested URI contains a query.
//client.Headers.Add ("ID", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
Console.WriteLine(s);
data.Close();
reader.Close();

我在

上遇到错误
Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID);

是否由于任何防火墙设置?

最佳答案

我遇到了类似的问题,必须使用以下方法将 URL 从字符串转换为 Uri 对象:

Uri myUri = new Uri(URLInStringFormat, UriKind.Absolute);

(URLInStringFormat 是您的网址) 尝试使用 Uri 而不是字符串作为连接:

WebClient client = new WebClient();
client.OpenRead(myUri);

关于c# - "A connection attempt failed because the connected party did not properly respond after a period of time"使用 WebClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693353/

相关文章:

c# - Mono WebClient 编码问题

C# Webclient Stream 下载文件从 FTP 到本地存储

c# - 只有数字的文本框

C# Hook 全局键盘事件 - .net 4.0

c# - 作为 Remote App 运行时应用程序未显示托盘图标

c# - 在 UWP 应用页面之间共享 xaml

javascript - 如何从 JavaScript 通过 TCP 套接字进行通信?

c++ - Windows Sockets 理解 recv 和 send

c++ - 部分udp文件传输

Java HTMLUnit WebClient ScriptException 错误