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)
{
    try
    {
        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;

https://api.foobar.com/foo?startDate=0001-01-01%2000%3A00%3A00&endDate=2020-02-17%2000%3A00%3A00

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?

Edit

.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("https://api.foobar.com/foo");
    FlushCache();
    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()
{
    DnsFlushResolverCache();
}

public static void FlushCache(string hostName)
{
    DnsFlushResolverCacheEntry(hostName);
}

来源:https://stackoverflow.com/questions/66042242/received-an-unexpected-eof-or-0-bytes-from-the-transport-stream-previous-answe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!