AnswerBun.com

HttpClient fails to connect to server in .Net Framework, succeeds in .Net Core; what's causing the failure?

Stack Overflow Asked by buttonblue on September 2, 2020

Edit:
Popped back in because I haven’t fixed the problem and it’s really starting to do my head in.

After multiple reinstallations of .Net Framework libraries and Visual Studio 2019, I opted for a complete laptop reimage, none of which made a difference.

A chance discovery meant I found both the Core and Framework code worked as expected when in the office. Just not at home; confirmed this by having my workmate bring his laptop over and the Framework version did not work when he connected to my home network (but it does when connected to his home network).

So yeah…router settings? I have no idea. I have installed Wireshark and I’m looking at the outputs, but I don’t really know how to interpret what I’m seeing.

Original:
I have a .Net Core 3.1 console application with the code below. It works just fine, returns the expected JSON output.

I created a .Net Framework 4.8 console application with the same code (literally copied and pasted). It doesn’t work. Instead of the expected JSON, I get an exception ("unable to connect to the remote server") with an inner exception of type System.Net.Sockets.SocketException. The message for the inner exception is "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 104.31.73.145:443". The exception is coming from the call to httpClient.GetAsync().

The code isn’t that complex! Given it does work with .Net Core, what should I be doing differently to use .Net Framework instead? I’ve tried setting up the HttpClient and HttpClientHandler with a variety of parameters, to no avail.

Neither the .Net Core, nor the .Net Framework projects have any additional packages added, other than those provided by default on project creation and those added to resolve references.

Edit: Windows 10, Visual Studio 2019 (16.6.4)

class Program
{
  private static HttpClient httpClient;

  static async Task Main(string[] args)
  {
    // Need credentials to get past work's proxy
    var handler = new HttpClientHandler
    {
        DefaultProxyCredentials = CredentialCache.DefaultCredentials
    };

    httpClient = new HttpClient(handler)
    {
        BaseAddress = new Uri("https://jsonplaceholder.typicode.com/")
    };

    try
    {
        HttpResponseMessage response = await httpClient.GetAsync("users/1");
        response.EnsureSuccessStatusCode();

        var statusText = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;
        Console.WriteLine(statusText);
        var responseBodyAsText = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBodyAsText);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
  }
}

Add your own answers!

Related Questions

Why are declarations in a switch construct legal?

1  Asked on November 12, 2021 by l-s-roth

 

How do I import InvalidURL exception in praw for python?

1  Asked on November 12, 2021 by johannes-pertl

   

Place button on bottom of a div

7  Asked on November 12, 2021 by jackal

       

Javascript: using tuples as dictionary keys

4  Asked on November 12, 2021 by hasen

     

Gmail REST api batch support for getting messages

3  Asked on November 12, 2021 by dune76

   

Custom Route Doesn’t Get The Query String

2  Asked on November 12, 2021 by at-2017

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir