Received an unexpected EOF or 0 bytes from the transport stream. (Previous Answers to the Same Question Doesn't Apply)

假如想象 提交于 2021-02-20 03:48:54


I get this InnerException with below Exception Tree;

My Code is;

public T GetQueryResultsFromQuery<T>(string query)
        HttpClient client = new HttpClient();

        System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

        var results = client.GetAsync(query).GetAwaiter().GetResult(); // error happens here

        return _jsonHelper.DeserializeObject<T>(results.Content.ReadAsStringAsync().GetAwaiter().GetResult());
    catch(System.Exception ex)
        throw ex;

And an example query string;

As one can see, I tried the solution ideas which were given on the similar questions but it doesn't work.

Same "query" works like a charm on Postman.

Is there any other solution ideas that might help my situation?


.NET Framework version is 4.7.2 and I just created a console app in which I don't have this issue whatsoever. Not sure what is really causing the problem on this project. I'd appreciate a help.

Edit 2

So, I have found an answer(?) which was to flush the cache. But even if it works on one startup, it fails if I relaunch the application. What is wrong here, I'm not sure.

The newest code part;

using (var httpClient = new System.Net.Http.HttpClient())
    System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 |
    SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

    ServicePointManager.DnsRefreshTimeout = 0;

    httpClient.DefaultRequestHeaders.ConnectionClose = true;

    var getter = httpClient.GetAsync("");
    var vuhu = getter.GetAwaiter().GetResult();

    return _jsonHelper.DeserializeObject<T>(vuhu.Content.ReadAsStringAsync().GetAwaiter().GetResult());

I get a succesfull result on some re-launches of the project;

And I get the same error on other re-launches;

Nothing code-wise changes between the relaunches, so what is the issue here?

Edit 3 The FlushCahce Function (from this answer);

[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCache")]1
static extern UInt32 DnsFlushResolverCache();

[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A")]
public static extern int DnsFlushResolverCacheEntry(string hostName);

public static void FlushCache()

public static void FlushCache(string hostName)