在Windows Phone 8.1中发布到服务时,从HRESULT获取异常:0x80072F0D [英] Getting Exception from HRESULT: 0x80072F0D when posting to a service in Windows phone 8.1

查看:168
本文介绍了在Windows Phone 8.1中发布到服务时,从HRESULT获取异常:0x80072F0D的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I am trying to post a data to an API using HttpClient in Windows Phone 8.1, I am always getting Exception from HRESULT: 0x80072F0D exception. In fiddler, it works fine.

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

请告诉我出了什么问题?

Please tell me what went wrong?

-FYI ----

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

For getting additional information about the HRESULT code : Follow this WebErrorStatus enumeration

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

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

推荐答案

这看起来像与证书有关的问题.也许您正在使用SSL.如果很多程序在没有明显必要的情况下(例如浏览器)优雅地覆盖丢失的证书,则HttpClient对此非常敏感.

This looks like a certificate related problem. Maybe you are using SSL. While lots of programs gracefully override missing certificates if not explicitly necessary (e.g.: browsers) the HttpClient is pretty sensitive against that.

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

You should try to download the certificate for the connection you're using and store the cert file in your assets folder. When your app starts, push it into the certificate store. This is a snippet I am using in one of my apps. Maybe this makes your exception go away.

在此处了解更多信息:

Read more here: 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;
}

这篇关于在Windows Phone 8.1中发布到服务时,从HRESULT获取异常:0x80072F0D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆