【问题标题】:Xamarin UWP HTTPS REST call returns gibberishXamarin UWP HTTPS REST 调用返回乱码
【发布时间】:2017-07-21 07:56:40
【问题描述】:

看来我在这里碰壁了,需要一双新的眼睛从不同的角度看待问题。

上下文

我有一个适用于 iOS、Android 和 UWP 的 Xamarin.Forms PCL 应用程序。 我需要调用 REST 服务;有些是纯 HTTP,有些是 HTTPS。

问题

对于 iOS 和 Android,一切正常。 对于 UWP,HTTP 调用工作正常,HTTPS 调用而不是预期的 JSON,返回乱码。这是一个示例:

"\u001f�\b\0\0\0\0\0\0��V\nK-�����W�244��Q\n-N-�K�MU�R O�+����I�ϪJ,(P�Q\n��\u0001I8��f�)�\u0002\02d+�>\0\0\0"

调用代码在 PCL 中使用 System.Net.Http 框架库(见下文)。

有没有人遇到过类似的问题?

        /// <summary>
    /// A generic api call to a REST Web service, with data to be sent.
    /// </summary>
    /// <typeparam name="TParam">The type of object for the data that will be sent, used by post</typeparam>
    /// <typeparam name="TResult">The type of object for the response of the server,expected to be of IClientApiModel type</typeparam>
    ///<param name="Uri">The service URI</param>
    /// <param name="isPost">Whether the API should be treated as POST (<code>true</code>) or as a GET (<code>false</code>).</param>
    /// <param name="param">The input data (can be null).</param>
    /// <param name="authorization">The authorization header for the REST service</param>
    /// <returns>An object of type TResult, and in case of Exception with the HasErrors property set to <code>true</code>.</returns>
    protected async Task<TResult> RequestAsync<TParam, TResult>(string Uri, bool isPost, TParam param, AuthenticationHeaderValue authorization)
        where TResult : IClientApiModel, new()
    {
        try
        {
            using (HttpClient client = new HttpClient())
            {
                using (HttpRequestMessage request = new HttpRequestMessage(isPost ? HttpMethod.Post : HttpMethod.Get, Uri))
                {
                    request.Headers.Authorization = authorization;

                    if (param != null)
                    {
                        var data = new StringContent(JsonConvert.SerializeObject(param));
                        request.Content = data;
                        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    }

                    using (HttpResponseMessage response = await client.SendAsync(request)) //(request))
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return new TResult() { HasErrors = true, HasNoConnection = true };
                            }
                            return new TResult() { HasErrors = true };
                        }
                        var content = await response.Content.ReadAsStringAsync();

                        TResult model = content.IsJson() ?
                            JsonConvert.DeserializeObject<TResult>(content) :
                            new TResult() { HasErrors = true };

                        return model;
                    }
                }
            }
        }
        catch (WebException weberror)
        {
            throw weberror;
        }
        catch (Exception e)
        {
            var def = new TResult() { HasErrors = true };
            return def;
        }
    }

【问题讨论】:

  • 澄清一下,您的共享库 (PCL) 中有代码使用 HttpClient 类返回某种数据,并且在您的 android 和 iOS 项目中数据看起来正确,但在您的 uwp 应用程序中返回的响应是您在上面的 OP 中列出的编码?
  • 是的。对于 HTTP 调用,它在 UWP 上也是正确的。只有 UWP+HTTPS 似乎是问题所在。它应该返回纯 JSON。我也尝试对其进行解码(UTF-8)但无济于事。
  • 您是否有公开的服务可供复制?如果是,请提供 uri 和身份验证
  • 你好尤里。很抱歉耽搁了这么久。不幸的是,这是为客户提供的实时服务,不能用于测试目的。仍然没有找到解决方案,但一位朋友指出了可能的证书问题的方向,因为它只发生在 HTTPS 上。还在折腾……

标签: rest https uwp xamarin.forms


【解决方案1】:

只是为了让大家知道......最后一切都归结为为 HttpClient 配置一个处理程序:

HttpClientHandler handler = new HttpClientHandler()
{
    AllowAutoRedirect = false
};
_httpClient = new HttpClient(handler);

【讨论】:

  • 哈...这只是部分解决方案。它在仅安装了 SSL2.0 的服务器上进行了测试。使用 TLS1.2 将它移到实时服务器后,好的 ol' 乱码字符串又回来了。也许这会缩小范围,但没有解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2019-03-10
相关资源
最近更新 更多