可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am working on a website, in which I am retrieving XML data from an external URL, using the following code
WebRequest req = WebRequest.Create("External server url");
req.Proxy = new System.Net.WebProxy("proxyUrl:8080", true);
req.Proxy.Credentials = CredentialCache.DefaultCredentials;
WebResponse resp = req.GetResponse();
StreamReader textReader = new StreamReader(resp.GetResponseStream());
XmlTextReader xmlReader = new XmlTextReader(textReader);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
This code is working fine on my development PC (Windows XP with .Net 3.5)
But when I deploy this code to IIS (Both at Windows XP and at Windows Server 2003) it's giving me following error
"The remote server returned an error: (407) Proxy Authentication Required."
Sometimes it gives me
"The remote server returned an error: (502) Bad Gateway."
Following code is from my web.config
<system.net>
<defaultProxy>
<proxy usesystemdefault="False" proxyaddress ="http://172.16.12.12:8080" bypassonlocal ="True" />
</defaultProxy>
</system.net>
Please help me ?
[Edit]
Even when i run the website for devlopment PC but through IIS it gives me error "The remote server returned an error: (407) Proxy Authentication Required."
But when i run website from Microsoft Devlopment server, it is running fine
回答1:
@Mohit Agarwal
Many thanks for suggesting adding ' useDefaultCredentials="true" ', you're a star!
I have been trying to get the .NET library for the Google Data API sample exe's working for weeks without success. Adding your suggestion fixed my problem and I now get a connection instead of 407 Proxy Authentication Required.
speadsheet.exe.config contents need to be:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="true"/>
</defaultProxy>
</system.net>
</configuration>
In my case NOT as Google suggest:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="true"/>
</defaultProxy>
</system.net>
</configuration>
http://code.google.com/p/google-gdata/wiki/WebProxySetup
回答2:
Does it work when you change the snippet in web.config to:
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="False" proxyaddress ="http://172.16.12.12:8080" bypassonlocal ="True" />
</defaultProxy>
</system.net>
回答3:
Beneath the root <configuration>
element in app.config
or Web.config
:
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="True"/>
</defaultProxy>
</system.net>
回答4:
Try this one if you want to specify the proxy details
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy proxyaddress="http://<ProxyServer>:<port>"
usesystemdefault="False"
bypassonlocal="True"
autoDetect="False" />
</defaultProxy>
</system.net>
</configuration>
Cheers!
回答5:
It might be helpful for someone out there finding this via Google that you can actually put this to use in .NET applications that may not have their own AppName.exe.config file yet (or you can modify it if so). We use NextGen EPM (medical scheduling / billing) and their credit-card processing system kept getting stuck at our proxy server because it wouldn't pass the credentials. I made an EXEName.config file (NextGenEPM.exe.config in this case) containing the snippet above:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy usesystemdefault="true"/>
</defaultProxy>
</system.net>
</configuration>
Success! This allowed me to resolve the issue without mucking around in our proxy server, which is adamantly configured to require authentication and we'd rather not compromise that.
回答6:
This is probably caused by the account that IIS is running under not having the appropriate permissions to get through the authenticating proxy.
When you run it on your development PC, you are running it as your logon, which I assume has permissions to get through the proxy. When running inside IIS, it is not running as you, and so probably cannot get through the proxy.
You could either give the IIS user permissions to get through the proxy (which will be unlikely to work in a domain environment as the IIS user will be a local user the machine), or configure your application to run as a network user with permissions to get through the proxy.
This can be done by either getting IIS to run as a domain user (I wouldn't recommend this approach), or by configuring you application to run as a domain user using web.config (see this article for more info on how to do this).
回答7:
We spent a long time struggling with this issue and updated our app.config to use default credentials as specified in the answers above.
However, it still didn't work!
Following a lot of pain we discovered that our app.config was not being included automatically with our click once application.
Simple mistake caused extreme head wreckedness!!!