Windows Phone 8.1 RT Windows.Web.Http.HttpClient在不同的身份验证响应中返回相同的数据

时间:2016-09-26 18:16:35

标签: windows-phone-8.1 httprequest windows-rt

我正在创建Windows Phone 8.1 RT应用程序(VS 2015社区)。 我选择了Windows.Web.Http.HttpClient与Web服务进行通信。

在一个页面上,我尝试使用以下代码从Web服务获取项目列表:

    public static async void GetItems(string username, string password, Action<ItemsModel, string, string> completion)
    {
            string methodUrl = "methodUrl";
            var resourceUri = new Uri(new Uri(baseUrl), methodUrl);
            var client = new HttpClient();
            var byteArray = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);
            var basicAuth = Windows.Web.Http.Headers.HttpCredentialsHeaderValue("Basic", Convert.ToBase64String(byteArray));

            client.DefaultRequestHeaders.Authorization = basicAuth;

            var response = new HttpResponseMessage();
            try
            {
                response = await client.GetAsync(resourceUri);
            }
            catch (Exception ex)
            {
            }

            if (response.IsSuccessStatusCode)
            {
                var responseContent = await response.Content.ReadAsStringAsync();
                var items = DeserializeMyItems(responseContent);
                completion(items, null, null);
            }
            else
            {
                //handle errors
            }
    }

当我使用一个用户凭据登录(基本身份验证)时,一切正常。但是当我尝试其他用户凭据(以及其后的所有其他凭据)时,我仍然得到与第一个相同的响应(就像响应没有刷新一样)。 我第一次启动应用时连接的每个帐户都会发生这种情况。在第一次成功响应后,我总是从该响应中获取数据(即使我登录其他有效的用户凭据)。

我已经检查了邮递员这个请求,我在那里看到问题不在网络服务中,而是在我身边。

我还检查了响应的标题,我看到所有后续响应在标题中都有相同的ID(X-Vcap-Request-Id)。

有人能指出导致这种行为的错误吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以创建HttpBaseProtocolFilter。像这样:

UBound