繁体   English   中英

如何在Webservice中使用HttpWebRequest检测受信任的站点?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM