1

Is it possible to use cts.Token when i'am reading async via httpClient?

Here is what i'm trying to do.

using (var client = new HttpClient()) { client.Timeout = TimeSpan.FromMilliseconds(20000); var response = await client.PostAsync("http://" + MyIp + ":9090/api/SqlAction/", new StringContent(postQuery, Encoding.UTF8, "application/json"), cts.Token); if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(cts.Token); } 

But i am taking Error CS1501 No overload for method 'ReadAsStringAsync' takes 1 arguments

1

1 Answer 1

1

No it isn't because the method ReadAsStringAsync hasn't an overloaded method which accepts a CancellationToken as argument!

You could check before if a cancellation was requested:

if (!cts.Token.IsCancellationRequested && response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any other method which i can read string async using cts token?
@ddd If you have a look into the documentation for the HttpContent Class there isn't any built-in method which accepts a ctx. I updated my answer a bit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.