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

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

问题描述

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

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

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

这是我的代码:

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

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

is it due any firewall setting?

推荐答案

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

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 是你的 URL)尝试使用 Uri 而不是字符串作为连接:

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

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

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

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