【问题标题】:HttpClient.PostAsync Xamarin.Android C# wrong responseHttpClient.PostAsync Xamarin.Android C# 错误响应
【发布时间】:2017-11-27 09:26:39
【问题描述】:

我正在编写一个 Xamarin.Android 应用程序,该应用程序通过 ADAL 身份验证将数据发布到 Net Core Web 应用程序。问题是 API 控制器已设置,因此它不需要身份验证 ([AllowAnonymous]),但我的一个也是唯一一个 httpClient.PostAsync() 方法响应 Microsoft 登录页面。我尝试使用 WebClient.UploadString() 并且响应是相同的登录页面。我正在从 UWP 移植这个应用程序,并且相同的代码在那里运行没有任何问题。

编辑: 添加一些代码以获取更多信息。问题是 response.IsSuccessStatusCode 是 true,但是 responseString 包含 Microsoft 登录页面,但它应该返回 json 数据。

  HttpResponseMessage response;
        try
        {


            response = await httpClient.PostAsync(url, new System.Net.Http.StringContent(JsonConvert.SerializeObject(data), System.Text.Encoding.UTF8, "application/json"));
        }
        catch (Exception ex)
        {


            return result;
        }
        if (response.IsSuccessStatusCode)
        {
            var responseString = await response.Content.ReadAsStringAsync();
            try
            {
                //parsing the response
            }
            catch (Exception)
            {
               //response was incorrect
            }
        }

【问题讨论】:

  • 张贴您的代码以便更好地理解......带有网址
  • 我认为问题在于 API 控制器而不是 xamarin 应用程序。看起来您正在正确调用其余服务
  • 奇怪的是,它不仅仅在 Xamarin Android 应用程序中工作。我已经从模拟器和实际设备尝试过,响应是我需要登录,但是当我从 UWP 应用程序和 Fiddler 发布数据时,它工作正常并且不要求身份验证。

标签: c# android xamarin asp.net-core httpclient


【解决方案1】:

试试这个:

HttpContent content = response.Content;
var json = await content.ReadAsStringAsync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多