0
votes

I am writing an MVC application which needs to request owner names for ip addresses from ARIN.net. Here is the snippet of program code that I have written to do this:

                                .
                                .
                                .

string requestUrl = "http://whois.arin.net/rest/ip/17.151.229.4";
WebResponse response = null;
WebRequest request = WebRequest.Create(requestUrl);
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
XElement responseElmn = XElement.Parse(sr.ReadToEnd());
                                .
                                .
                                .

This snippet of code is successfully able to send a request to http://whois.arin.net/rest/ip/17.151.229.4 and receive the appropriate response when it is run from a console application on my machine. However, when I attempt to put this snippet of code in my MVC application, I get an exception stating

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond .

My local machine, where these applications run, is inside the network of a very large corporation and therefore this snippet must send its request to ARIN through a proxy server when it is run by the previously mentioned applications. I believe that I'm probably receiving this discrepancy in results due to the console application running under a different user than the MVC application. I'm running both applications in debug mode in Visual Studio 2013. The MVC application is hosted by IIS Express when it is run in debug mode.

My questions are:

  1. How can I find out what user the console application and the MVC application are run under?

  2. How can I configure IIS Express to run the MVC application under a different user?

I have been unable to find answers to the questions in my research, any help is appreciated.

1
I do not set the proxy server in either the console application or the MVC application, yet the console app is able to successfully obtain a response and the MVC app is not. I assume that both of them are using the same proxy server. Were you suggesting that I set a different proxy server for the MVC application?Alva A
That's because the console app is running in the context of you the user. The MVC application is running under a different identity. I suggest that you would need to specifically set the credentials for the MVC application.Brendan Green

1 Answers

0
votes

I discovered the cause of the problem. The MVC application didn't have a proxy server assigned to the proxy object member of the WebRequest instance by default, while the one in the console application did.

I therefore had to assign the proxy server to the proxy object instance of the WebRequest instance in my code before attempting to retrieve a response:

string requestUrl = "http://whois.arin.net/rest/ip/17.151.229.4";
WebResponse response = null;
WebRequest request = WebRequest.Create(requestUrl);


Uri newUri = new Uri("http://proxy.bigcorp.com:8080");
WebProxy myProxy = new WebProxy(newUri);
request.Proxy = myProxy;


request.Proxy.Credentials = CredentialCache.DefaultCredentials;
response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
XElement responseElmn = XElement.Parse(sr.ReadToEnd());

Thank you Brendan Green for your help!