【问题标题】:How to detect trusted sites using HttpWebRequest in webservice?如何在 Web 服务中使用 HttpWebRequest 检测受信任的站点?
【发布时间】:2013-01-12 12:48:39
【问题描述】:

我有一个 C# 2.0 框架的控制台应用程序,使用 vs 2005. 这很奇怪,我在我的电脑和服务器上工作的代码完全相同(使用 web 服务)。

当我在 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;
}

~~~~~~~~~~~~~

我已经解决了。

每次调用每个站点时都会更改代理信息。所以我在下面添加了一些行。 Reference site

 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?您是否尝试过使用 fiddler 或 wireshark 来查看正在发出的请求?
  • “受信任的站点”是 IE 现象,而不是 WebRequest 现象。该消息可能来自您的 Internet 代理服务器。
  • 我尝试在浏览器中点击 URL。没问题。
  • 我尝试使用wireshark进行捕获。但没什么特别的。 [日志] HTTP GET aaa.bbb.com/pages/default.aspx HTTP/1.1 HTTP HTTP/1.1 404 Not Found (text/html)
  • 我已经解决了。那是代理设置。每当更改请求的站点时,代理 uri 都会更改,因此我更改了未创建站点的代理 uri。 Reference site : [link]

标签: c# web-services httpwebrequest http-status-code-404


【解决方案1】:

尝试从浏览器(在服务器上)访问您的 url - 如果它有效,则您的浏览器代理配置为与给定端口与外部世界进行通信。

检查您的防火墙策略以及他们阻止与外界建立连接的任何特定用户代理字符串?

如果您从浏览器尝试,还要验证您的代理身份验证(我假设这里是 IE)。浏览器可能会自动提供它/NTLM 身份验证字符串。您的程序可能无法做到的地方?

【讨论】:

  • 问题是每个网站的代理服务器 ip 都发生了变化。所以我将我的源代码编辑为代理服务器和网站之间的匹配。
猜你喜欢
  • 2010-09-20
  • 2018-12-22
  • 1970-01-01
  • 2020-07-03
  • 2011-02-28
  • 1970-01-01
  • 2011-04-05
  • 2011-03-07
  • 1970-01-01
相关资源
最近更新 更多