httclient sendasync issue in .net core

Ajayakumar GS 1 Reputation point
2021-08-11T01:53:35.793+00:00
    var client = new HttpClient();
        HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.nseindia.com/option-chain");
        httpRequestMessage.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        httpRequestMessage.Headers.Add("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        httpRequestMessage.Headers.Add("Accept-Language", "en-us,en;q=0.5");
        httpRequestMessage.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        var response = await client.SendAsync(httpRequestMessage);

The above code is working in the .net console application but not working in the .net core console application. In .net core console application getting following error.

IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request,

SocketException: The I/O operation has been aborted because of either a thread exit or an application request.

Developer technologies | ASP.NET Core | Other

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 85,456 Reputation points
    2021-08-11T19:44:41.583+00:00

    The error appears to indicate your main() which called the httpcliebt code without waiting for the async operation to complete and the program exited before the call completed.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

  2. AgaveJoe 31,706 Reputation points
    2021-08-11T10:20:42.933+00:00

    These error can be due to port exhaustion due to not using HttpClient as recommended in the official docs.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.