I have this c# MVC code and it works fine with the GetAsync and GetPost methods, but when using GetStringAsync, it freezes up at the line:
version = await client.GetStringAsync("/API/Version");
Driver code:
Task<string>[] tasks = new Task<string>[count];
for (int i = 0; i < count; i++)
{
tasks[i] = MyHttpClient.GetVersion(port, method);
}
Task.WaitAll(tasks);
string[] results = new string[tasks.Length];
for(int i=0; i<tasks.Length; i++)
{
Task<string> t = (Task<string>)(tasks[i]);
results[i] = (string)t.Result;
}
HttpCilent code:
public static async Task<string> GetVersion(int port, string method)
{
try
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:" + port);
string version = null;
if (method.ToUpper().Equals("GETSTR"))
{
version = await client.GetStringAsync("/API/Version");
}
else if (method.ToUpper().Equals("GET"))
{
HttpResponseMessage res = client.GetAsync("/API/Version").Result;
version = res.Content.ReadAsStringAsync().Result;
}
else if (method.ToUpper().Equals("POST"))
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("name", "jax")
});
HttpResponseMessage res = client.PostAsync("/API/Version", content).Result;
version = res.Content.ReadAsStringAsync().Result;
}
client.Dispose();
return version;
}
catch (Exception ex)
{
return "Error: " + ex.Message;
}
}
}
Any ideas why?