简体   繁体   English

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

[英]How to detect trusted sites using HttpWebRequest in webservice?

I have a console application for the 2.0 framework in C#, using vs 2005. This is weird, the exact same code I have worked on both my pc and the server(using webservice). 我有一个使用vs 2005的C#2.0框架的控制台应用程序。这很奇怪,我在PC和服务器上都使用过相同的代码(使用Webservice)。

When I execute HttpWebRequest.GetResponse on URL, First one(pc) works well but last one(webservice) returns an error : 当我在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. 

I don't know why? 不知道为什么

This is the code: 这是代码:

 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; } 

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

I've solved. 我解决了

The Proxy information was changed whenever each site called. 每当每个站点都调用时,代理服务器信息就会更改。 So I added some lines below. 所以我在下面添加了几行。 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;
    }

Try Accessing your url from Browser (on-server) - If it works your browser agent is configured to make communication to/from given ports to the outside world. 尝试从浏览器(在服务器上)访问您的url-如果工作正常,则将浏览器代理配置为与给定端口进行通讯,或与外界进行通讯。

Check your firewall policies and any specific user-agent strings they are preventing to make connections to the out-side world ? 检查您的防火墙策略以及它们阻止与外部世界建立连接的任何特定用户代理字符串?

Also verify your Proxy Authentication , if you try it from the browser (i presume here its IE ) . 如果从浏览器尝试代理身份验证,也请验证您的代理身份验证(我在这里假设其是IE)。 browser might supply it automatically / NTLM Authentication strings. 浏览器可能会自动提供它/ NTLM身份验证字符串。 where your program might not be able to ? 您的程序可能无法执行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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