Windows Phone 8.1 通用应用程序中的 HttpClient [英] HttpClient in Windows Phone 8.1 Universal app

查看:33
本文介绍了Windows Phone 8.1 通用应用程序中的 HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在测试通用应用程序并达到了这个目标:

So i'm testing universal applications and have reached this:

我在 Windows 平板电脑上有一个从服务器获取数据的应用程序.

I have an application on Windows Tablet that gets data from server.

服务器受证书 (SSL) 保护

Server is protected with certificate ( SSL )

我有这个代码,在简单的 Windows 商店应用程序和平板电脑的通用应用程序项目上运行良好,但不是电话

I have this code, runs great on simple Windows Store Application, and Universal application project for tablet, but not Phone

async public static Task<string> GetDataFromServer()
    {
        try
        {
            HttpClientHandler aHandler = new HttpClientHandler();
            aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
            HttpClient aClient = new HttpClient(aHandler);
            aClient.DefaultRequestHeaders.ExpectContinue = false;
            aClient.DefaultRequestHeaders.MaxForwards = 3;
            Uri requestUri = new Uri("MYURL");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
            var result = await aClient.GetAsync(requestUri, HttpCompletionOption.ResponseContentRead);
            var responseHeader = result.Headers;
            var responseBody = await result.Content.ReadAsStringAsync();
            return responseBody;
        }
        catch (Exception e)
        {
            return "";
        }
    }

所有需要的功能都在清单中设置:

All needed capabilities are set in manifest:

<Capabilities>
<Capability Name="internetClient" />
<Capability Name="sharedUserCertificates" />
<Capability Name="privateNetworkClientServer" />
<Capability Name="internetClientServer" />
 </Capabilities>
 <Extensions>
<Extension Category="windows.certificates">
  <Certificates>
    <Certificate StoreName="Root" Content="Assets\CoolCertificate.cer" />
  </Certificates>
</Extension>

电话代码 - 不会崩溃,没有错误或警告 - 只是空白结果.

Phone code - doesn't crash, no errors or warnings - just blank result.

代码: {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Content-Length: 0}}没有像 WinRT 那样向用户发出通知:

Code: {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{ Content-Length: 0}} And no notification to the user like in WinRT:

推荐答案

我希望您拥有用作 PFX 所需的证书.在这种情况下,请参考我在链接中的回复

I hope you have the certificate that you need to use as PFX. In that case please refer to my reply in the link

客户端证书 Windows Phone 8.1

这篇关于Windows Phone 8.1 通用应用程序中的 HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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