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

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

 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) { // } 

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

您应该尝试下载正在使用的连接的证书,并将cert文件存储在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服务器证书错误。 忽略归类为Ignorable或Fatal的服务器证书错误可能会导致通过SSL会话传递的内容失去隐私或完整性。

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

netsh winhttp reset proxy