gpt4 book ai didi

c# - "A connection attempt failed because the connected party did not properly respond after a period of time"使用 WebClient

转载 作者:行者123 更新时间:2023-11-29 16:01:48 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com