问题
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?
回答1:
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
Right click on the console app and select add service reference.
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.
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>
回答2:
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" />
回答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 Service
object 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
回答4:
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"();
来源:https://stackoverflow.com/questions/7386452/add-web-service-reference-from-behind-a-proxy-server