class MyHttpClientClass
{
public async static string Getrequest(string url, string port)
{
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await client.GetAsync("http://" + url + ":" + port + "/"))
{
using (HttpContent content = response.Content)
{
return await content.ReadAsStringAsync();
}
}
}
}
}
This source code gives me a compile-time error:
The return type of an async method must be
void,Task, orTask<T>
How can I return a string from this method without using out or ref?
I also want to get rid of async/await pattern.
async/await. You shouldn't be creating a new HttpClient each time either, it's thread-safe and meant to be reused. Reusing it means you don't have to perform DNS resolution for every request too, which can really speed up performance*_httpClient.GetStringAsync(someUrl)HttpClientis intended to be instantiated once and re-used throughout the life of an application. Instantiating anHttpClientclass for every request will exhaust the number of sockets available under heavy loads. (citation)