我正在使用以下在本地机器上运行的代码,但是当我在服务器上尝试相同的代码时它会抛出错误
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/