“连接尝试失败,因为被连接方在一段时间后未正确响应”。使用WebClient [英] "A connection attempt failed because the connected party did not properly respond after a period of time" using WebClient

查看:152
本文介绍了“连接尝试失败,因为被连接方在一段时间后未正确响应”。使用WebClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


连接尝试失败,因为连接的一方在一段时间后未正确响应,或者建立的连接
因连接的主机未能响应而失败


这是我的代码:

  WebClient客户端= new WebClient() ; 
//添加用户代理标头,以防
//请求的URI包含查询。
//client.Headers.Add( ID, Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;));
流数据= 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();

我在



<$ p $上遇到错误p> 流数据= client.OpenRead( http:// + Request.ServerVariables [ HTTP_HOST] + Request.ApplicationPath + /PageDetails.aspx?ModuleID= + ID);

是否有任何防火墙设置?

解决方案

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

  Uri myUri =新的Uri(URLInStringFormat,UriKind.Absolute); 

(URLInStringFormat是您的URL)
尝试使用Uri而不是字符串进行连接:

  WebClient客户端= new WebClient(); 
client.OpenRead(myUri);


I am using the following code which is working on local machine, but when i tried the same code on server it throws me error

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

Here is my code:

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();

I am getting error on

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

is it due any firewall setting?

解决方案

I had a similar problem and had to convert the URL from string to Uri object using:

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

(URLInStringFormat is your URL) Try to connect using the Uri instead of the string as:

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

这篇关于“连接尝试失败,因为被连接方在一段时间后未正确响应”。使用WebClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆