C# 从代理服务器后面调用 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/859224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a webservice from behind a proxy server
提问by Hemant
I need to add a functionality in an application (C#) which will use a web service (XML SOAP service).
我需要在将使用 Web 服务(XML SOAP 服务)的应用程序 (C#) 中添加一个功能。
Now this application can (and mostly) be used in an corporate environment which has a proxy server in place.
现在,该应用程序可以(并且主要)用于具有代理服务器的企业环境中。
I understand the SOAP services use HTTP protocol and hence should use port 80, which is normally kept opened. Is it right that application can use web service without any special coding or I will need to write special code to detect proxy settings or some other issues you see?
我了解 SOAP 服务使用 HTTP 协议,因此应该使用端口 80,该端口通常保持打开状态。应用程序可以在没有任何特殊编码的情况下使用 Web 服务是否正确,或者我需要编写特殊代码来检测代理设置或您看到的其他一些问题?
EDIT: Webservice is a publicly available service on internet. Its not on same network.
编辑:Webservice 是 Internet 上的一项公开可用服务。它不在同一个网络上。
采纳答案by Hemant
OK. So I did some experiments and it turns out that we do need to write some code to make it work from behind the proxy server. (Though I would have prefered a better solution)
好的。所以我做了一些实验,结果证明我们确实需要编写一些代码才能让它在代理服务器后面工作。(虽然我更喜欢更好的解决方案)
So it actually drills down to asking proxy server details from user and then configure the service proxy class for proxy server as below:
所以它实际上深入到向用户询问代理服务器的详细信息,然后为代理服务器配置服务代理类,如下所示:
var networkCredentials = new NetworkCredential ("username", "password", "domain"); WebProxy myProxy = new WebProxy ("W.X.Y.Z:NN", true) {Credentials = networkCredentials}; var service = new iptocountry { Proxy = myProxy }; string result = service.FindCountryAsString ("A.B.C.D");
I wrote a test class and it uses IP To Countryfree web service.
我写了一个测试类,它使用IP To Country免费网络服务。
Using above code, I could consume the web service successfully.
使用上面的代码,我可以成功使用 Web 服务。
回答by jgallant
If your webservice is on the same internal network as the client calling the webservice, then it shouldn't be going through a proxy.
如果您的 web 服务与调用 web 服务的客户端在同一内部网络上,那么它不应该通过代理。
回答by Eric Petroelje
As long as web traffic (port 80) is allowed through, you shouldn't need to do anything special. From a router / proxy server's perspective web service calls are the same as any other HTTP traffic.
只要允许网络流量(端口 80)通过,您就不需要做任何特殊的事情。从路由器/代理服务器的角度来看,Web 服务调用与任何其他 HTTP 流量相同。
回答by AaronS
It will use port 80 by default, and you shouldn't have to do any further coding.
默认情况下,它将使用端口 80,您不必再进行任何编码。
If you do need to go through a proxy of some sort, all you need to do is add the following to your web.config:
如果您确实需要通过某种代理,您需要做的就是将以下内容添加到您的 web.config 中:
<system.net>
<defaultProxy>
<proxy proxyaddress="http://yourproxyserver:80" />
</defaultProxy>
</system.net>
You could also do it through code using this:
您也可以使用以下代码通过代码来完成:
WebRequest.DefaultWebProxy = new WebProxy("http://yourproxyserver:80/",true);
回答by Marc Gravell
The inbuilt code (WebClient, WCF, HttpWebRequest, etc) all make use of the WinHTTP configuration to obtain proxy configuration. So all you need to do is configure WinHTTP to know about the proxy!
内置代码(WebClient、WCF、HttpWebRequest 等)都利用 WinHTTP 配置来获取代理配置。因此,您需要做的就是配置 WinHTTP 以了解代理!
In XP, this is:
在 XP 中,这是:
proxycfg -u
which imports the settings from the user's IE proxy settings (WinInet).
它从用户的 IE 代理设置 (WinInet) 中导入设置。
On Vista / etc, you use
在 Vista / 等上,您使用
netsh winhttp
(and some subcommand like "import")
(和一些子命令,如“导入”)
untested, but try:
未经测试,但请尝试:
netsh winhttp import proxy source=ie
After that, your .NET code should all work via the proxy that the uses has presumably already configured in order to use IE etc.
之后,您的 .NET 代码都应该通过用户可能已经配置为使用 IE 等的代理工作。
回答by kusnaditjung tjung
You can use the default setting from you local machine:
您可以使用本地计算机的默认设置:
System.Net.ServicePointManager.Expect100Continue = false;
wsclient.Proxy= System.Net.HttpWebRequest.GetSystemWebProxy();
wsclient.Proxy.Credentials = CredentialCache.DefaultCredentials;
and in app.config add this configuration:
并在 app.config 添加此配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
</configuration>