9

I am facing this error :

The remote name could not be resolved: 'russgates85-001-site1.smarterasp.net'

When I request html contents to read using web client it gives me error. Below is my code.

string strHTML = string.Empty;
WebClient myClient = new WebClient();
UTF8Encoding utf8 = new UTF8Encoding();
byte[] requestHTML;
string pdfFileName = "outputpdf_" + DateTime.Now.Ticks + ".pdf";
string webUrl = Request.Url.Scheme + "://" + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port : "");

requestHTML = myClient.DownloadData("http://russgates85-001-site1.smarterasp.net/adminsec/images/printinvoice.aspx?iid=2");

// OR

requestHTML = myClient.DownloadData(webUrl + "?iid=3");

When I put the same URL on my local code/environment it works fine.

Mehran
  • 1,409
  • 2
  • 17
  • 27
k-s
  • 2,192
  • 11
  • 39
  • 73
  • Is your Webserver where you run your ASP.NET code also your local dev machine or some other box? – Yuriy Galanter May 18 '13 at 21:22
  • FYI you can also get this if you're making a request to a site that doesn't have a valid certificate if it's running under https. (i.e. the IIS Express Development Certificate). Hitting it in your browser and accepting the warning interstitial will allow it to load. – benmccallum Oct 19 '16 at 04:40
  • 1
    As [Ken Johnson said](https://stackoverflow.com/questions/16626348/the-remote-name-could-not-be-resolved-webclient/24293932#24293932) you need to use the server IP instead of the server name. – Lvcios Dec 22 '17 at 19:16

4 Answers4

7

Most likely the other location you run the code on indeed does not have access to that remote location. I.e. in many corporate environment servers aren't allowed outside Internet access. You may want to try to ping/traceroute russgates85-001-site1.smarterasp.net from that other server and if there's no access - configure router/firewall (open port etc.) or use proxy

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Ok, But website already hosted on above URL where all pages works. Only fetching content using above url is not working using web client. – k-s May 19 '13 at 04:26
  • That's different. Client`s workstation is accessing the Internet to reach the above URL. And in your code it's the server itself trying to do the same. Many policies don't allow that, especially via external URL. You may need to try local server name instead or even simple `localhost` - this might work since request is local to the server itself – Yuriy Galanter May 19 '13 at 15:02
  • Ok, so for server/live.. what I should do to make it run? currently it gives me error I mentioned. – k-s May 19 '13 at 18:43
  • Try using `http://localhost/adminsec/images/printinvoice.aspx?iid=2` as download URL in that code block – Yuriy Galanter May 19 '13 at 19:14
  • locally as I said working fine even if I write live url. but on live it gives me error. I mentioned url for live as well – k-s May 20 '13 at 08:29
  • 2
    No you do not understand. Have the URL as 'localhost' in code (not in browser address bar) even for remotely deployed site - in that case it will be used instead of 'russgate...' – Yuriy Galanter May 20 '13 at 11:25
  • I tried with writing live URL in localhost site running in local environment and tested which worked in local environment. – k-s May 20 '13 at 12:10
  • No. Again: inside of your code, that performs the download keep the URL as `requestHTML = myClient.DownloadData("http://localhost/adminsec/images/printinvoice.aspx?iid=2");` *even* when the code is deployed and running at russgate. – Yuriy Galanter May 20 '13 at 13:14
  • I tried writing localhost on live url but still it is not working. writing localhost gives 404 error. Any luck? – k-s May 22 '13 at 06:02
  • 1
    Ok then the path from the local box is different and you have to use external URL. Can you talk to the host provider - can they make it possible to resolve server name from the server itself? It's just a matter of config. – Yuriy Galanter May 22 '13 at 14:40
  • Yes, Thanks for your inputs. Currently URL is like http://russgates85-001-site1.smarterasp.net/adminsec so I have to tell them to give servername of above URL? – k-s May 22 '13 at 16:06
  • You should ask them how to make server resolve DNS name `russgates85-001-site1.smarterasp.net` into IP address `205.144.171.8` even when the call is made from the server itself. In effect server should be able to browse istself. – Yuriy Galanter May 22 '13 at 16:33
  • Hi, It is working fine after making changes in DNS name to point to main site with A pointing in DNS names. Thanks. – k-s May 28 '13 at 07:22
  • Glad it worked for you. I've been in the similar situation and know how frustrating this can be, especially if actual code working correctly – Yuriy Galanter May 28 '13 at 13:42
  • Yes, thanks. saw your site of blog. good explored.. m working on asp.net mvc and related techs. sql my favourite one. – k-s May 28 '13 at 21:00
6

I ran into this and needed to use the IP of the remote server rather than the DNS name. Production was trying to access the remote server using its DNS name, which was not viable on that side of the firewall.

Ken Johnson
  • 330
  • 3
  • 5
1

I had the same issue and got it resolved by setting the Proxy for the webclient

explicitly like

 webClient.Proxy = new WebProxy("myproxy.com:portnumber"); 
 byte[]  bytearr= webClient.DownloadData(acsMetadataEndpointUrlWithRealm);
Sundara Prabu
  • 2,361
  • 1
  • 21
  • 20
1

By default it will take system proxy.

To solve this, force to set your proxy in web.config or .cs api webclient.

Code fix should be like following, In under System.net section in web.config

<defaultProxy>

<proxy proxyaddress="http://0.000.000.000:00" bypassonlocal="True" usesystemdefault="False" autoDetect="False" />

</defaultProxy>
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103