1

I have a piece of old code (a single .aspx file) that I need to get through a proxy. This code used to work, but now the company have tightened up on security.

The offending line of code is:

dataSet.ReadXml(url);

The url is https.

It is running on .NET version 2.0 - this cannot be upgraded. I cannot change the web.config file.

What do I need to add to the .aspx file to get it to work? The error I am getting is:

The remote server returned an error: (407) Proxy Authentication Required.

There is no "connecting to the web" code in the script.

EDIT

Based on Dan's comment, I have tried this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
IWebProxy proxy = request.Proxy;                    
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://10.79.30.190:8080");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the 
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("username", "password");
request.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(responseString);

but am still getting the same error

EDIT

Another attempt:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
IWebProxy proxy = request.Proxy;                    
WebProxy myProxy = new WebProxy();
Uri newUri = new Uri("http://10.79.30.190:8080");
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the 
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential("username", "password");
request.Proxy = myProxy;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(new StreamReader(response.GetResponseStream()));

but am still getting the same error

Graham
  • 7,807
  • 20
  • 69
  • 114
  • You will need to fetch the file using [`HttpWebRequest`](https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx) or similar, specifying proxy credentials, and then parse it as XML. See https://stackoverflow.com/a/9603791/3608792. – Dan Wilson Aug 03 '18 at 13:57
  • have you tried default credentials? `request.Proxy.Credentials = CredentialCache.DefaultCredentials;` – Alex Terry Aug 06 '18 at 20:29

1 Answers1

1

it means, that your credentials for the proxy server are incorrect, best solution to try and approach this problem would be:

First, add this line to your Web.Config:

<system.net>
    <defaultProxy useDefaultCredentials="true" >
    </defaultProxy>
</system.net>

Second, is through code:

service.Proxy = WebRequest.DefaultWebProxy;
service.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
service.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

third, is to set the credentials in two locations through code:

HttpWebRequest webRequest = WebRequest.Create(uirTradeStream) as HttpWebRequest;
webRequest.Proxy = WebRequest.DefaultWebProxy;
webRequest.Credentials = new NetworkCredential("user", "password", "domain");
webRequest.Proxy.Credentials = new NetworkCredential("user", "password", "domain");

it's whatever suits you best here.

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 1
    Turns out it was a firewall issue. I switched proxies (I'm guessing on a different firewall) and it worked. The error message in this case is misleading, but I'll give you the points for trying to help anyway :-) – Graham Aug 07 '18 at 11:18