tags:

views:

872

answers:

2

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

A: 

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>
Mohit Agarwal
Change it to what?
Charlie Somerville
@Mohit, Already i am writing CredentialCache.DefaultCredentials; inside source code, SO i don't think it make some difference?
Hemant Kothiyal
A: 

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).

adrianbanks