作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用以下在本地计算机上运行的代码,但是当我在服务器上尝试相同的代码时,它会抛出错误
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/
我是一名优秀的程序员,十分优秀!