add web service reference from behind a proxy server

人盡茶涼 提交于 2019-11-28 21:40:06

Take a look at the links below for specifying the proxy address and server port when adding a web reference.

http://msdn.microsoft.com/en-us/library/bb628649.aspx

http://msdn.microsoft.com/en-us/library/03seed2h.aspx

To add a reference to an asmx

  1. Right click on the console app and select add service reference.

  2. Click on the advanced button and enter the asmx address in the address bar. Click on the green button next to it to discover the asmx.

  3. Give it a name and click on add ref.

Update: try updating web config/ app config and add;

<system.net>

<defaultProxy>
<proxy usesystemdefault="True" proxyaddress="http://[your proxy address and port number]"  bypassonlocal="True"/>

</defaultProxy>

</system.net>

I spent almost 50 hours finding the problem, could not find anywhere on the web this simple solution.

Under "configuration" section in Web.config add this:

  <system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>

Then works like a charm!

You can also do it from the code behind:

serviceConnection = new WebService1();
serviceConnection.Proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
serviceConnection.Proxy.Credentials = CredentialCache.DefaultCredentials; 

Works beautiful!!.

If you need to consume from HTTPS location add this configuration:

<message clientCredentialType="Certificate" algorithmSuite="Default" />

Adding the Reference:

Make sure that you are adding the Reference like this. You need to click on "Add Service Reference", go to "Advanced" and finally click on "Add Web Reference".

Then add the following:

http://***/service1.asmx

For port 8080 you use:

http://***:8080/service1.asmx

Setup the Proxy for your Web Service:

To make sure that the Web Service is using your Internet Explorer proxy you can add the following to your Web Serviceobject on your client application.

webService1.Proxy = WebRequest.GetSystemWebProxy();

You can also set up the Proxy manually:

webService1.Proxy = new WebProxy("hxxp://my-proxy-settings:8080/");

NTLM

If you use NTLM you will probably need to make sure that you use the Default Credentials on your client project as well. You can easily do this by passing it in when creating the Web Serivce using UseDefaultCredentials set to true.

public webService _webService = new webService() { UseDefaultCredentials = true };

You can also disable NTLM Authentication for your Web Service project. You can do this under Project Properties -> Web. If you uncheck this option you should be able to add the Web Service without having to authenticate.

http://msdn.microsoft.com/en-us/library/aa378749.aspx

I cannot automatically create web service reference using vs2010. I decide to use wsdl.exe tool, and in parameter named /parameters pass xml file with proxy server credentials

wsdl.exe http://service uri/service1.asmx /parameters:c:\temp\wsdlparameters.xml

WSDL.exe generate a file Service1.cs (default). I add this file to my project and use it like this :

WebProxy wp = new WebProxy(@"YourProxyServer",ProxyPort);
wp.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
Service1 service1 = new Service1();
service1.Proxy = wp;
service1."YourServiceMethod"();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!