从代理服务器后面使用Web服务

时间:2013-11-12 23:22:45

标签: c# proxy

是的,另一个问题是如何:从代理服务器后面使用Web服务。

好的,所以我知道这个问题已在本论坛的其他地方以及网上的其他地方得到解答。但由于某种原因,我的设置无法正常工作。所以这是我的情况:

我正在使用Visual Studio 2010 Express通过WSDL生成的.CS文件(不使用Web引用)连接到Web服务。

当我尝试调用一个简单的Ping()函数时(如下所示),我收到以下错误:

WebException未被清除:请求失败,HTTP状态为407:需要代理身份验证。

我使用的代码如下所示。我不明白为什么我会收到这个错误。当我提示我输入代理凭据时,我使用的是与Web浏览器相同的用户名和密码。

任何建议都会非常感谢!!!!

提前致谢。

            PingResponseDocument theResponse;

            WebProxy wp = new WebProxy("IP_ADDRESS:PORT_NO", true);
            wp.Credentials = new NetworkCredential("USER_NAME", "PASSWORD", "IP_ADDRESS:PORT_NO");

            WebService test = new WebService();
            PingRequestDocument doc = new PingRequestDocument();

            test.Proxy = wp;
            theResponse = test.ping(doc);

2 个答案:

答案 0 :(得分:0)

请试一试。

  1. 按Ctrl + Shift + A并创建一个新的应用程序配置文件,如果您已经添加了。
  2. 现在在app.config中添加system.net元素,如下所示。

    <?xml version="1.0" encoding="utf-8" ?>
     <configuration>
      <system.net>
       <defaultProxy useDefaultCredentials="true" />
      </system.net>
    </configuration>
    

答案 1 :(得分:0)

更新:

所以我找到了问题的答案。基本上,当您需要使用Web服务时,应始终检查它是否需要代理。您可以使用以下函数执行此操作:

isbypassed()

如果返回true,则表示您不需要代理凭证,可以使用以下内容:

WebProxy p = new WebProxy();
WebService w = new WebService(); // Your Web Service.
p.BypassList = new string[] {"IP_ADDRESS:PORT_NUMBER/dir../dir"};
w.Proxy = p;

w.someFunctionofYours(); // the function you call.

干杯