HTTP status 407: Proxy authentication required Error when calling web service - c#

I have spent two hours on this error "HTTP status 407: Proxy authentication required Error " when calling a web service, my code is like following
WebProxy oWebProxy = new System.Net.WebProxy(ProxyServer, ProxyPort);
oWebProxy.Credentials = new NetworkCredential(ProxyUser,ProxyPassword,ProxyDomain);
oserv.Proxy = oWebProxy;
oserv.Credentials = new NetworkCredential(theusername, thepassword);
I have verified that the proxy address, user id password are all correct, and I could access the web service thru the IE in same pc, but when I run the code with VS, the error keeps popup.
I have tried the UserDefaultCredentials=true as well, but no luck.
Any idea?

Popping a<defaultProxy /> element into in app.config / web.config under <system.net> with useDefaultCredentials="true" may well do the job.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>

Try to run the application in on same network where the webservice is installed

Related

C# 407 Proxy Authentication Required

I want to make a request to a resource (that resource is behind a proxy). I have the proxy address and the port as well. I have tried with NetworkCredentialn no success, with CacheCredentials no success. WebException is:
ProtocolError
The remote server returned an error: (407) Proxy Authentification Required
I always get error at this line:
WebResponse response = request.GetResponse();
I have already done this:
Package manager in Visual Studio 2015 "407 (Proxy Authentication Required)"
I tried to configure my App.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>
From our corporate network, we usually employ this code:
WebProxy proxy = new WebProxy("http://your.proxy.server:8080", true);
proxy.Credentials = new NetworkCredential("user", "password");
WebRequest.DefaultWebProxy = proxy;
The idea is you put this code somewhere at the beginning of your program (or in the App start if you're on IIS) and then every single request will take the default proxy configuration.
No change in web.config is required. AFAICT, in web.config you cannot set the credentials.
In my experience, it works also for web services and WCF communications.

defaultProxy.useDefaultCredentials in WCF client programmatically

What code I should write to get same behavior as I set in config file
<system.net>
<defaultProxy useDefaultCredentials="true"></defaultProxy>
</system.net>
This work for me:
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;

WebRequest.Proxy - What is the default value

Background: We have a service installed at a remote site that has started failing to call an external webservice. According to the network\support engineers on site no proxy should be required, but when attempting to execute the WebRequest an exception is returned:
System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
The exception is relatively self-explanatory, but looking at the code, no Proxy is specified in the call to the WebRequest and no proxy is defined in the app.config file.
Referring to the MSDN documentation for WebRequest.Proxy:
"The IWebProxy object to use to proxy the request. The default value is set by calling the GlobalProxySelection.Select property."
and then referring to the documentation for GlobalProxySelection.Select:
"Gets or sets the global HTTP proxy."
And given that GlobalProxySelection.Select is not set anywhere in the application, where does the default value come from? And are there any other global config files where the global HTTP proxy may be set?
You have to set the proxy manually. You can do it by doing the following, if you want to use the Default system proxy. (Internet settings in windows)
myRequest.Proxy = WebRequest.GetSystemWebProxy();
You are getting the error because your network is blocking access to the server directly without using a proxy.
While this doesn't solve your problem directly, can I recommend trying a <defaultProxy /> element in your app.config / web.config under <system.net> with useDefaultCredentials="true" - this may well get your application authenticating through the proxy and therefore not requiring a special bypass rule at all.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true" />
</system.net>
</configuration>

The remote server returned an error: NotFound. Silverlight + WCF

I'm trying to call web service for few hours.
I have added clientaccesspolicy.xml:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
and crossdomain.xml:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
</cross-domain-policy>
to root directory of my web site.
Web service is called from IIS, and it's working here.
But when I'm trying to call my wcf web service from silverlight application I get this error:
The remote server returned an error: NotFound.
This is log from Fiddler:
a:InternalServiceFaultThe server was unable to process the request due to
an internal error. For more information about the error, either turn
on IncludeExceptionDetailInFaults (either from
ServiceBehaviorAttribute or from the <serviceDebug>
configuration behavior) on the server in order to send the exception
information back to the client, or turn on tracing as per the
Microsoft .NET Framework 3.0 SDK documentation and inspect the server
trace logs.
This is code, when I get an error:
public int EndUserExist(System.IAsyncResult result) {
object[] _args = new object[0];
int _result = ((int)(base.EndInvoke("UserExist", _args, result))); //Here
return _result;
}
What am I doing wrong?
Look in your web.config for this text: IncludeExceptionDetailInFaults
set that to true. Then run your service request again and watch Fiddler this time it will tell you what you need to know.
May be anything from a SQL error to a null reference. :-)
All right ;)
Now it's working.
I turned on debug in web.config so I got exception message in Fiddler.
I need to set identity user in pool on IIS for sql database.
In your mobile device, go to wifi settings and edit current wifi connection.
Set proxy "Automatically detect settings".

getting "remote server cannot be resolved" on localhost does not get fixed by defaultProxy?

here is the sitution, i am testing on my localhost from my machine at home (no proxy server and windows default firewall) and retrieving api.flickr.com xml file, when I come to work (that uses an ISA server to connect) I get "remote server could not be resolved" so I added these lines to the web.config
<system.net>
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://localhost"
bypassonlocal="True"
/>
<bypasslist>
<add address="[a-z]+\.flickr\.com\.+" />
</bypasslist>
</defaultProxy>
</system.net>
that returns:System.Net.WebException: The remote server returned an error: (404) Not Found.
what went wrong? thanks
There are two possible scenarios here:
1: If you are building a client app (e.g. Console or WinForms) and want to access http://localhost using WebClient or HttpWebRequest without any intervening proxies, then bypassonlocal="True" should accomplish this. In other words, your app.config should look like this:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
2: if, however, you're trying to get your ASP.NET app (running on http://localhost) to be able to correctly resolve URIs either with a proxy or without one, then you'll need to set up proxy info correctly in your web.config (or in machine.config so you won't have to change your app's web.config), so ASP.NET will know that you are running a proxy or not running one. Like this:
Home:
<defaultProxy>
<proxy
usesystemdefault="False"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
Work:
<defaultProxy>
<proxy
usesystemdefault="False"
proxyaddress="http://yourproxyserver:8080"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
It's also possible to use proxy auto-detection, to pick up settings from the registry, etc. but I've always shied away from those approaches for servers apps... too fragile.
BTW, if you find that things are configured correctly, and you still get the error, the first thing I'd recommend is to code up a quick test which manually sets the proxy before your WebClient/HttpWebRequest call, instead of relying on configuration to do it. Like this:
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebClient wc = new WebClient();
wc.Proxy = proxyObject;
string s = wc.DownloadString ("http://www.google.com");
If the requests don't go through your work proxy correctly even when you're using code, even if the proxy is correctly configured in your code, then the proxy itself may be the problem.
In WebClient to download data from local no issues but downloading from internet is problem so configure the following
In your Web.config add below lines and replace your Intenet proxy Address and port
<system.net>
<defaultProxy useDefaultCredentials="true" enabled="true">
<proxy usesystemdefault="False" proxyaddress="http://your proxy address:port" bypassonlocal="True" />
</defaultProxy>
<settings>
<servicePointManager expect100Continue="false" />
</settings> </system.net>
now your program logic work for downloading content from internet and public URLS.

Categories