10

I'm using vs2010. In a simple console app I try to add a service reference to http://***/service1.asmx , old asmx service. My computer is behind a proxy server, so i get an error :

"The remote server returned an unexpected response: (407) Proxy Authentication Required."

When im using wsdl tool i can not define proxy server port number and i get message that server, for examle 10.0.0.3:80, did not respond, but i need to specify 8080 port and don't know how. How could i create a reference?

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
Nikita
  • 173
  • 1
  • 2
  • 16

4 Answers4

28

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" />
Israel Margulies
  • 8,656
  • 2
  • 30
  • 26
8

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>
WooHoo
  • 1,912
  • 17
  • 22
  • when i do so i got an error Operation is not valid due to the current state of the object. – Nikita Sep 12 '11 at 10:54
  • I have tried to add this settings to app.config and the error is still occurred. When i add web reference I can see service description. I have to hyperlinks to 2 methods of service. Also i configure IE options for proxy. – Nikita Sep 12 '11 at 11:17
  • this is just a guess, but try https rather than http when adding the asmx. – WooHoo Sep 12 '11 at 11:20
  • when using https i get "This program cannot display the webpage" page in "Add web reference" window – Nikita Sep 12 '11 at 11:43
  • Yes, I've tried to do so but not succeed, and i do not know what else to try to configure to make it work automatically. =( – Nikita Sep 15 '11 at 05:08
3

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

eandersson
  • 25,781
  • 8
  • 89
  • 110
  • upd: web service, which i trying to use in my console app has got anonymous authentication. In my console app i can't find Project Properties -> Web. – Nikita Sep 12 '11 at 10:37
  • This would be under your Web Service, and not under the client. – eandersson Sep 12 '11 at 10:39
  • "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." - AS I've mentioned early, the web service has Anonymous Authentication. So i can open web service page via my browser. When i open service uri using browser, it(browser) show me a window to input username and password. I provide only Proxy Server credentials. – Nikita Sep 12 '11 at 11:48
  • Did you try adding set UseDefaultCredentials to true? If that doesn't work you could try to set the proxy to the IE default proxy: `_webService.Proxy = WebRequest.GetSystemWebProxy();` – eandersson Sep 12 '11 at 11:54
  • You can also set the proxy settings manually like this: `_webService.Proxy = new WebProxy("hxxp://my-proxy-settings:8080/");` – eandersson Sep 12 '11 at 11:55
2

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"();
Nikita
  • 173
  • 1
  • 2
  • 16
  • Glad you got there in the end. – WooHoo Sep 13 '11 at 09:27
  • Did you take a look at my last comment? It *should* do this automatically for you, based on your IE settings. – eandersson Sep 13 '11 at 15:53
  • i do not know what else to configure to make it work automatically. Maybe our proxy block some kind of traffic but i think the http enough to add reference. – Nikita Sep 16 '11 at 04:52