HttpClient in Windows Phone 8.1 Universal app

北城以北 提交于 2019-12-08 08:16:49

问题


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

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

Server is protected with certificate ( SSL )

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.

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:


回答1:


Please use aHandler.UseDefaultCredentials= true; property and try once.hope that may solve the issue?




回答2:


Note that in Windows Phone 8.1, there's two HttpClient classes. One is inside System.Net.Http namespace, and the other Windows.Web.Http namespace.

You want to use the one in Windows.Web.Http.




回答3:


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

Client Certificates Windows Phone 8.1



来源:https://stackoverflow.com/questions/23290505/httpclient-in-windows-phone-8-1-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!