[英]How to detect trusted sites using HttpWebRequest in webservice?
我有一個使用vs 2005的C#2.0框架的控制台應用程序。這很奇怪,我在PC和服務器上都使用過相同的代碼(使用Webservice)。
當我在URL上執行
HttpWebRequest.GetResponse
時,第一個(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;
}
嘗試從瀏覽器(在服務器上)訪問您的url-如果工作正常,則將瀏覽器代理配置為與給定端口進行通訊,或與外界進行通訊。
檢查您的防火牆策略以及它們阻止與外部世界建立連接的任何特定用戶代理字符串?
如果從瀏覽器嘗試代理身份驗證,也請驗證您的代理身份驗證(我在這里假設其是IE)。 瀏覽器可能會自動提供它/ NTLM身份驗證字符串。 您的程序可能無法執行?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.