在Windows phone 8.1中向服务提交时出现HRESULT异常:0x80072F0D

3

当我尝试使用Windows Phone 8.1中的HttpClient向API发布数据时,我总是会收到HRESULT:0x80072F0D异常。在fiddler中,它可以正常工作。

try
{  
    var requestbody="json data"
    HttpClient httpClient = new HttpClient();
    HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), new Uri(addressUri));
    msg.Content = new HttpStringContent(requestbody);
    msg.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/json");
    HttpResponseMessage response = await httpClient.SendRequestAsync(msg).AsTask();
}           
catch (Exception ex)
{
    getting **Exception from HRESULT: 0x80072F0D**
}

请告诉我出了什么问题?
---FYI----
如需获取有关 HRESULT 代码的其他信息:请参阅此 WebErrorStatus 枚举
  var exceptionDetail = WebError.GetStatus(ex.GetBaseException().HResult);

  if (exceptionDetail == WebErrorStatus.HostNameNotResolved)
  {
    //
  }

你使用的是哪个 HttpClient?还有异常信息是什么? - Fred
4
知悉,该 HRESULT 错误码为 WININET_E_INVALID_CA,“证书颁发机构无效或不正确”。 - Decade Moon
@Fred 我正在 Windows Phone 8.1 xaml 项目中使用 Windows.Web HttpClient。 - asitis
@DecadeMoon 十年月光,我需要澄清什么?或者说我需要对此做些什么?在 Fiddler 中,它正常工作。我正在使用相同的URL和相同的POST数据。 - asitis
https?可能是SSL证书问题吗? - Oliver
显示剩余6条评论
4个回答

6
这似乎是一个与证书相关的问题。也许你正在使用SSL。虽然许多程序在不明确需要证书的情况下优雅地覆盖缺失的证书(例如:浏览器),但HttpClient对此非常敏感。
您应该尝试下载您正在使用的连接的证书,并将证书文件存储在资产文件夹中。当您的应用程序启动时,将其推送到证书存储中。这是我在其中一个应用程序中使用的片段。也许这可以消除您的异常。
在此处阅读更多信息:http://blogs.msdn.com/b/wsdevsol/archive/2014/06/05/including-self-signed-certificates-with-your-windows-runtime-based-windows-phone-8-1-apps.aspx
// Add our custom certificate
try
{
    // Read the contents of the Certificate file
    System.Uri certificateFile = new System.Uri("ms-appx:///Assets/ca.cer");
    Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(certificateFile);
    Windows.Storage.Streams.IBuffer certBlob = await Windows.Storage.FileIO.ReadBufferAsync(file);

    // Create an instance of the Certificate class using the retrieved certificate blob contents
    Windows.Security.Cryptography.Certificates.Certificate rootCert = new Windows.Security.Cryptography.Certificates.Certificate(certBlob);

    // Get access to the TrustedRootCertificationAuthorities for your own app (not the system one)
    Windows.Security.Cryptography.Certificates.CertificateStore trustedStore = Windows.Security.Cryptography.Certificates.CertificateStores.TrustedRootCertificationAuthorities;

    // Add the certificate to the TrustedRootCertificationAuthorities store for your app
    trustedStore.Add(rootCert);
}
catch (Exception oEx)
{
   // Catch that exception. We don't really have a choice here..
   var msg = oEx.Message;
}

这很有道理。如果需要提供证书,我会与我们的服务器团队进行核实,并明天更新您结果。 - asitis

1
你可以尝试使用以下代码来绕过这个错误:
var baseFilter = new HttpBaseProtocolFilter();
baseFilter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidCertificateAuthorityPolicy);
var httpClient = new HttpClient(baseFilter);

这只是让错误消失而不是解决问题。我对SSL错误并不太了解,这可能不是一个安全的选项,并且可能无法通过应用程序认证。根据文档:
SSL服务器证书错误只应在高级场景中忽略。忽略被分类为可忽略或致命的服务器证书错误可能会导致SSL会话传递的内容的隐私或完整性丢失。

此外,在 Windows Phone 项目中,我不得不使用“Untrusted”而不是“InvalidCertificateAuthorityPolicy”,否则会引发关于未知选项值的异常。 - Yury Schkatula

0

收到与发起者相同的错误。我不使用代理。

这对我有用。netsh winhttp reset proxy 下一个传真顺利发送,没有错误。


-1
经历了0x80072efd问题。花费了我数小时甚至数天的时间来解决。给出即时解决方案的方法是从管理员命令提示符中运行以下命令:
netsh winhttp reset proxy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接