代码之家  ›  专栏  ›  技术社区  ›  An Hoa

使用HttpClient异步下载:找不到与此错误代码关联的文本

  •  1
  • An Hoa  · 技术社区  · 10 年前

    我打算使用以下代码下载文件。它在WIFI可用时工作;但当没有Wifi时,我希望能抓住 previousTask.get() 不幸地 catch 在我的代码中似乎没有捕捉到异常。例外情况是 HRESULT:0x80072F30 The text associated with this error code could not be found. 顺便说一句我是不是错过了一些不可计算的例外?

    auto httpClient = ref new HttpClient();
    auto get_operation = httpClient->GetAsync(ref new Uri(url), HttpCompletionOption::ResponseContentRead);
    get_operation->Progress = progressHandler;
    auto response = create_task(get_operation).then([](task<HttpResponseMessage^> previousTask)
    {
        try
        {
            return previousTask.get();
        }
        catch (Exception^ ex)
        {
            // Some how this does not catch
            OutputDebugString(("Exception: " + ex->Message)->Data());
            return (HttpResponseMessage^)nullptr;
        }
    }).get();
    // At this point, I expect either a fully read response or response=nullptr
    // Code to write to file is omitted
    

    编辑 :~~我测试了微软官方的 HttpClient sample 其显然使用类似的代码。显然,当没有网络连接时,该应用程序也会发生同样的崩溃。这种情况证实了缺陷存在于操作系统端,对此我们无能为力~~

    编辑 :事实证明,我认为该异常没有被捕获,因为Visual Studio会弹出一个对话框,我认为这意味着实际上该异常会导致应用程序崩溃,即当它未通过VS启动时;按对话框上的[继续]按钮进入 接住 条款从“开始”菜单启动应用程序没有问题。

    1 回复  |  直到 10 年前
        1
  •  3
  •   Peter Torr    10 年前

    如果从UI线程调用此代码,则删除 get() 从代码的最后一行调用。在UI线程中不能这样做。

    否则,当飞机模式打开时,你的代码对我来说很好;正如预期的那样,我在处理程序中捕捉到异常。异常具有 HResult 属于 0x80072f30 ,记录在 MSDN page http_NO_CM_CONNECTION中的错误