0

I’m trying to connect to a https REST service using C# as the client code. I can POST and GET from the service using POSTMAN but in order to do that I have to setup proxy settings specifying the company’s proxy address, user ID and password in POSTMANs settings. The C# code is .net 4 and I am using HttpClient in order to call the https service. However, I get Unable to connect to remote server. It’s not clear what the error is caused by. I am running the C# code as an ASP.NET app locally on my desktop as http://localhost calling across to the https service on the company network. So, is it the case because it is https I have to have a certificate for the https service installed locally and referenced in the client code? Or is it more likely to need something like what is setup in POSTMAN related to proxy in my C# code.

Sample code below:

   public async Task RunAsync()
    {
    // Not real url
        string url = "https://blah blah";

        HttpResponseMessage httpResponse = await GetAsync(url);
    }

    public async Task<HttpResponseMessage> GetAsync(string uri)
    {
        HttpClient httpClient = new HttpClient();
        HttpResponseMessage content = await httpClient.GetAsync(uri);
        return await Task.Run(() => content);
    }

Results from Exception thrown: InnerException = {"Unable to connect to the remote server"} Message = "An error occurred while sending the request." HResult -2146233088

David S
  • 103
  • 1
  • 6
  • First double check the host address and port numbers. – Stefan Sep 30 '20 at 13:50
  • thanks for your reply. host address, in fact the whole url has just been copied in from the one used in POSTMAN so I'm pretty sure its not a spelling mistake or a transposition error. – David S Sep 30 '20 at 13:53
  • Can it be a firewall issue at the host? Just asking because in 99% of all cases this is a simple connection issue – Stefan Sep 30 '20 at 14:04
  • What is the response you are getting? What does your code look like? – insane_developer Sep 30 '20 at 14:07
  • Are you an admin? To run VS as Admin you need to create a shortcut to VS. Then start VS by right click shortcut and select As Admin. – jdweng Sep 30 '20 at 14:26
  • Yes running Visual Studio 2019 in Admin Mode. – David S Sep 30 '20 at 15:00
  • if the API doesnt have the cors configuration for localhost or your ip I will not work, for calling a web from another web you will need to configure a reverse proxy. And Postman always works because it is not a browser client. [cors-with-postman](https://stackoverflow.com/questions/36250615/cors-with-postman) – Cristina Carrasco Sep 30 '20 at 15:13
  • [more info about cors](https://medium.com/@abderrahman.hamila/cors-is-not-your-nightmare-but-6cbc749400cf) – Cristina Carrasco Sep 30 '20 at 15:15
  • Try adding System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; (or TLS 1.3) before the HTTPClient. You probably are using TLS 1.0/1.1 which ill not work any more due to a Security update Microsoft release in June. – jdweng Sep 30 '20 at 15:47
  • thx. I already had System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 in there, I also tried : System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 but that didn't work either. Does anyone know whether you definitely need a SSL certificate installed locally to talk to an https service? – David S Oct 01 '20 at 04:50

0 Answers0