我在 C# 中有一个 2.0 框架的控制台应用程序,使用 vs 2005。这很奇怪,我在我的电脑和服务器上工作的代码完全相同(使用 web 服务)。
当我
HttpWebRequest.GetResponse
在 URL 上执行时,第一个(pc)运行良好,但最后一个(webservice)返回错误:(404) Not Found. The URL is a trusted site. Internet sites and local intranet sites are detected successfully. But trusted sites only could not be detected.
我不知道为什么?
这是代码:
private bool getSiteConnStatus(string url) { bool result = true; Uri uri = new Uri(url); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); WebProxy SCProxy = new WebProxy("123.123.123.123"); SCProxy.Credentials = new NetworkCredential(); request.Proxy = SCProxy; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response == null || response.StatusCode != HttpStatusCode.OK) { result = false; } response.Close(); } catch (Exception ex) { result = false; } return result; }
~~~~~~~~~~
我已经解决了。
每当每个站点调用时,代理信息都会更改。所以我在下面添加了一些行。 参考网站
private bool getSiteConnStatus(string url)
{
bool result = true;
Uri uri = new Uri(url);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
***WebProxy SCProxy;
if (request.Address.Host == "test.net")
{
SCProxy = new WebProxy("111.111.111.111", 8080);
}
else
{
SCProxy = new WebProxy("123.123.123.123", true);
}
request.Proxy = SCProxy;***
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
result = false;
}
response.Close();
}
catch (Exception ex)
{
result = false;
}
return result;
}