【发布时间】:2015-12-08 00:57:57
【问题描述】:
我得到了以下信息:
public async Task<bool> IsAuthenticatedAsync()
{
using (HttpClient client = new HttpClient())
{
using (MultipartFormDataContent content = new MultipartFormDataContent())
{
content.Add(new StringContent(this.Username, Encoding.UTF8), "username");
content.Add(new StringContent(this.Password, Encoding.UTF8), "password");
try
{
HttpResponseMessage message = await client.PostAsync(authenticatedUrl, content);
if (message.StatusCode == HttpStatusCode.Accepted)
return true;
return false;
}
catch(HttpRequestException)
{
System.Windows.Forms.MessageBox.Show("Some text.");
}
}
}
return false;
}
其中authenticatedUrl 是一些静态Uri。
现在假设网络服务器不可用(或地址错误)await client.PostAsync(authenticatedUrl, content) 抛出 HttpRequestException,因此是 try-catch。
问题是异常没有被捕获。我尝试关闭Just My Code,但这只是按照here 的建议添加了其他异常(即SocketException),并且仍然没有让catch 处理异常。
为什么没有捕获到异常?
编辑
主窗体(GUI):
public GUI(...)
{
...
CheckLoginState(username, password);
...
}
private async void CheckLoginState(string username, string password)
{
User user = new User(username, password);
if (user.Username != null && user.Password != null && await user.IsAuthenticatedAsync())
abmeldenToolStripMenuItem.Enabled = true;
}
【问题讨论】:
-
你有没有试过抓
AggregateException -
您确定正在抛出
HttpRequestException吗?尝试启用第一次机会例外 -
我在调试器中获得了
System.Net.Http.HttpRequestException(第一次机会)。 -
@IvanStoev 我做了,仍然抛出了
System.Net.Http.HttpRequestException,而try-catch没有捕获到。 -
"显示调试器异常窗口,如果你没有捕获异常" -- 除非在 Debug/Exceptions... 设置中你已经告诉调试器总是中断异常。鉴于您描述的代码和操作,这是您关注的最有可能的解释。如果您检查了设置并确定它们与您期望的一样,则需要提供a good, minimal, complete code example 以可靠地重现问题。您显示的代码中没有任何内容可以暗示除预期结果之外的任何内容。
标签: c# async-await httpclient