Ram*_*ngh 29 c# sockets webclient
我正在使用以下代码在本地机器上工作,但当我在服务器上尝试相同的代码时,它会抛出我的错误
连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应
这是我的代码:
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();
Run Code Online (Sandbox Code Playgroud)
我收到了错误
Stream data = client.OpenRead("http://" + Request.ServerVariables["HTTP_HOST"] + Request.ApplicationPath + "/PageDetails.aspx?ModuleID=" + ID);
Run Code Online (Sandbox Code Playgroud)
是因为任何防火墙设置?
bar*_*a_d 15
我有类似的问题,不得不使用以下方法将URL从字符串转换为Uri对象:
Uri myUri = new Uri(URLInStringFormat, UriKind.Absolute);
Run Code Online (Sandbox Code Playgroud)
(URLInStringFormat是您的URL)尝试使用Uri而不是字符串进行连接:
WebClient client = new WebClient();
client.OpenRead(myUri);
Run Code Online (Sandbox Code Playgroud)
Mah*_*hat 13
明确设置代理地址web.config解决了我的问题
<system.net>
<defaultProxy>
<proxy usesystemdefault = "false" proxyaddress="http://address:port" bypassonlocal="false"/>
</defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)
在使用Web服务时解决"TCP错误代码10060:连接尝试失败..."
小智 8
我知道这张票已经过时了,但我刚刚遇到了这个问题,我想我应该发布发生在我身上的事情以及我如何解决它:
在我打电话的服务中,有一个对另一个网络服务的调用。就像一个傻瓜一样,我在发布 Web 服务时忘记确保 DNS 设置是正确的,因此我的 Web 服务在发布时尝试从 api.myProductionserver.local 调用,而不是从 api.myProductionServer.com 调用。导致超时的是后端 Web 服务。
无论如何,我想我会把这个传递下去。