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 是您的 URL) 尝试使用 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/56130245/

相关文章:

一台服务器中的 Java TCP 和 UDP 回显

c# - C#.NET客户端和C服务器之间的套接字编程

c# - 使用 WebClient 下载 torrent 文件,导致文件损坏

c# - 代码块内的任意且无意义的字符串

C 处理多种类型的UDP包

c# - System.Text.Json 中 JObject.FromObject 的对应项是什么

c# - 使用 WebClient、DownloadData 方法和自定义 header 发布到网页

windows-phone-7 - 如何忽略 Windows Phone 7 中的证书错误?

c# - 处理同一文件的多个流的最佳方法

c# - 如何使用 Objective-C 桥将 "completion handler"从 Unity 传递到 Swift