ASP - running in localhost - unable to reach internet
-
25-09-2019 - |
Question
I have an ASP MVC controller action. I'm trying to make a web request
public ActionResult Index()
{
WebRequest request = HttpWebRequest.Create("http://www.example.com");
WebResponse response = request.GetResponse();
string str = response.ToString();
}`
I get a "WebException occured" the remote name could not be resolved: 'www.example.com'
If I start Fiddler, then the webrequest works.
I tried adding:
<system.net>
<defaultProxy>
<proxy usesystemdefault ="True" bypassonlocal="True" />
</defaultProxy>
to Web.config (with and without hte bypassonlocal) and it still doesn't work.
Any suggestions?
Solution
Try specifying the proxy server explicitly:
<system.net>
<defaultProxy>
<proxy proxyaddress="http://proxy.yourcompany.com:80" />
</defaultProxy>
</system.net>
You could also set the proxy programatically:
request.Proxy = new WebProxy("http://proxy.yourcompany.com:80", true);
When you set usesystemdefault
to true
, the application uses the proxy defined in the Internet Options
dialog box. When you deploy your application in IIS it usually executes under the Network Service
account which has very limited privileges, it doesn't even have any GUI session so it cannot infer the proxy server.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow