从 HRESULT 获取异常:在 Windows Phone 8.1 中发布到服务时0x80072F0D



当我尝试在Windows Phone 8.1中使用HttpClient将数据发布到API时,我总是得到Exception from HRESULT: 0x80072F0D异常。在小提琴手中,它工作正常。

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**
}

请告诉我出了什么问题?

---仅供参考----

要获取有关 HRESULT 代码的其他信息:请遵循此 WebErrorStatus 枚举

  var exceptionDetail = WebError.GetStatus(ex.GetBaseException().HResult);
  if (exceptionDetail == WebErrorStatus.HostNameNotResolved)
  {
    //
  }

这看起来像是与证书相关的问题。也许您正在使用SSL。虽然如果没有明确必要(例如:浏览器),许多程序会优雅地覆盖丢失的证书,但HttpClient对此非常敏感。

您应该尝试下载正在使用的连接的证书,并将证书文件存储在 assets 文件夹中。当应用启动时,将其推送到证书存储中。这是我在我的一个应用程序中使用的片段。也许这会让你的例外消失。

在此处阅读更多内容: 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;
}

您可以使用以下代码绕过错误:

var baseFilter = new HttpBaseProtocolFilter();
baseFilter.IgnorableServerCertificateErrors.Add(Windows.Security.Cryptography.Certificates.ChainValidationResult.InvalidCertificateAuthorityPolicy);
var httpClient = new HttpClient(baseFilter);

不过,这只会消除错误,而不是解决问题。我对 SSL 错误不太了解,这可能不是一个安全的选择,并且可能无法通过应用程序认证。根据文档:

只有在高级方案中,才应忽略 SSL 服务器证书错误。忽略分类为"可忽略"或"致命"的服务器证书错误可能会导致通过 SSL 会话传递的内容失去隐私或完整性。

收到与

发起方相同的错误。 我不使用代理。

这对我有用。 netsh winhttp 重置代理下一份传真传输无误。

遇到了0x80072efd问题。花了我几个小时甚至几天来解决。提供即时解决方案的解决方案是来自管理命令提示符的以下命令:

netsh winhttp reset proxy

最新更新