0

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);
    }
  }
}
  • Have you run Wireshark or similar and compared the two? – mjwills Jul 31 '20 at 00:05
  • 3
    _It works fine on my machine._ – mjwills Jul 31 '20 at 00:12
  • 1
    Cannot reproduce, works fine in Framework 4.8 and Core 3.1. – aepot Jul 31 '20 at 00:45
  • Same. The Server can push TLS1.3, but it doesn't require it, it falls back nicely to TLS1.2. It doesn't even ask for the more recent cipher suites (thus, it can also work in Windows 7) HSTS (which can cause this) is disabled, with or without specifying a `User-Agent` header. You can try to validate the Server certificate(s) to see whether you get there before it stops responding. Set `ServicePointManager.Expect100Continue = false;` – Jimi Jul 31 '20 at 01:03
  • Just found that it works on two coworker's laptops too. I've uninstalled/reinstalled .Net Framework 4.8 but that hasn't made a difference. – buttonblue Jul 31 '20 at 02:21
  • 1
    It works on ends. It might be a proxy setting in your environment. Check the following for possible solutions: https://stackoverflow.com/questions/17693353/a-connection-attempt-failed-because-the-connected-party-did-not-properly-respon – Jamal Jul 31 '20 at 03:19
  • I've tried setting up a ServicePointManager with no change in results, and if it was a proxy setting then I would expect the .Net Core version to fail too. I'm starting to think this has to be something not related to the code, but I'm at a loss as to what it could be... – buttonblue Jul 31 '20 at 04:35
  • Can you connect (and send request) to IP:port without C# (for example using postman or fiddler)? – Dmitry Aug 06 '20 at 05:56
  • It's possible that your IP range used to belong to someone naughty that your ISP decided to block... https://whatismyipaddress.com/blacklist-check ? – Jeremy Lakeman Aug 06 '20 at 06:25
  • Possible missing proxy info see [.net - C# HttpClient doesnt resolve host when used from Web Api project](https://stackoverflow.com/questions/47222773/c-sharp-httpclient-doesnt-resolve-host-when-used-from-web-api-project) – ΩmegaMan Mar 10 '22 at 19:28

0 Answers0