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

asi*_*tis 3 c# httpclient windows-8.1 windows-phone-8.1

当我尝试使用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**
}
Run Code Online (Sandbox Code Playgroud)

请告诉我出了什么问题?

--- FYI ----

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

  var exceptionDetail = WebError.GetStatus(ex.GetBaseException().HResult);

  if (exceptionDetail == WebErrorStatus.HostNameNotResolved)
  {
    //
  }
Run Code Online (Sandbox Code Playgroud)

Fre*_*red 6

这看起来像证书相关的问题.也许您正在使用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;
}
Run Code Online (Sandbox Code Playgroud)