等待httpClient.SendAsync(httpContent)没有响应 [英] await httpClient.SendAsync(httpContent) is non responsive
问题描述
正在等待httpClient.SendAsync(httpContent)
没有响应,尽管我发现代码/URL中没有错误,但仍然挂起.请提出建议/帮助.
await httpClient.SendAsync(httpContent)
is not responding though I found no error in code/url its still getting hang. Please suggest/help.
我的代码如下:
public async Task<string> Get_API_Result_String(string url, List<KeyValuePair<string, string>> parameters)
{
string res = "";
try
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//Prepare url
Uri mainurl = new Uri(settings[FSAPARAM.UserSettingsParam.SERVERNAME].ToString());
Uri requesturl = new Uri(mainurl, url);
var httpClient = new HttpClient();
var httpContent = new HttpRequestMessage(HttpMethod.Post, requesturl);
// httpContent.Headers.ExpectContinue = false;
httpContent.Content = new FormUrlEncodedContent(parameters);
HttpResponseMessage response = await httpClient.SendAsync(httpContent);
var result = await response.Content.ReadAsStringAsync();
res = result.ToString();
response.Dispose();
httpClient.Dispose();
httpContent.Dispose();
}
catch (Exception ex)
{
Logger l = new Logger();
l.LogInfo("Get_API_Result_String: "+ url + ex.Message.ToString());
ex = null;
l = null;
}
return res;
}
在另一个类中调用它,如下所示:
Calling it in another class as follows:
NetUtil u = new NetUtil();
string result = await u.Get_API_Result_String(Register_API, values);
u = null;
推荐答案
我预计,在您的调用堆栈的更深处,您正在对返回的任务
.这将导致僵局,我对此进行了详细解释在我的博客上.
I predict that further up your call stack, you are calling Wait
or Result
on a returned Task
. This will cause a deadlock that I explain fully on my blog.
总而言之, await
将捕获一个上下文,并使用该上下文恢复 async
方法;在UI应用程序上,这是一个UI线程.但是,如果UI线程被阻止(在对 Wait
或 Result
的调用中),则该线程不可用于恢复 async
方法
To summarize, await
will capture a context and use that to resume the async
method; on a UI application this is a UI thread. However, if the UI thread is blocked (in a call to Wait
or Result
), then that thread is not available to resume the async
method.
这篇关于等待httpClient.SendAsync(httpContent)没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!