在访问外部Web服务(.NET 2.0)之前获取代理配置 [英] Get Proxy configuration before accessing an external webservice (.NET 2.0)

查看:58
本文介绍了在访问外部Web服务(.NET 2.0)之前获取代理配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试通过Internet调用外部Web服务上的方法时,它引发了我
远程服务器返回了一个错误:(407)需要代理身份验证。

When trying to invoke a method on an external webservice (over the Internet) it throws me "The remote server returned an error: (407) Proxy Authentication Required."

为解决此问题,我使用以下代码设置了我们在办公室使用的代理:

To solve this, I used the following code to set the proxy we use in the office:

//Set the system proxy with valid server address or IP and port.
System.Net.WebProxy pry = new System.Net.WebProxy("MyHost", 8080);

//The DefaultCredentials automically get username and password.
pry.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.WebRequest.DefaultWebProxy = pry;

这很好,但是现在...我需要做一些少麻烦的尝试我系统中的信息,而不是手动设置。

That works fine, but now... I need to do that "less harcoded" trying to get the information from my system instead of setting that manually.

推荐答案

这将使用我认为(不弃用)IE的默认代理:

This will use whatever the default proxy is for IE I believe (not deprecated):

Services.MyService service = new Services.MyService();
service.UseDefaultCredentials = true;
service.Proxy = new System.Net.WebProxy();
service.Proxy.Credentials = service.Credentials;

这篇关于在访问外部Web服务(.NET 2.0)之前获取代理配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆