将HttpResponseMessage的内容转换为对象

cop*_*lii 5 c# system.net win-universal-app

我的问题:我该怎么做?

所以,直到本周,我还没有碰到任何关于.Net的东西.我忘记了很多,甚至更多我从未知道的事情,虽然我喜欢async/await关键字的想法,但我在实现客户端API实现的以下要求时遇到了一些问题:

  1. 所述ServerAPI类具有用于每个的API方法的方法中,采取适当的输入参数(例如,该方法Login需要在idpassword,使得该API调用,并将结果返回给调用者).
  2. 我想抽象出JSON,以便我的API方法返回你正在获取的实际对象(例如Login上面的方法返回一个User带有auth令牌的对象,uid等)
  3. 一些API方法在成功时返回204或没有有意义的内容(在我的用例中没有意义,也许我只关心成功/失败),因为我想要返回bool(true= success)或状态代码.
  4. 我想保留async/await(或等效)设计,因为到目前为止它似乎真的很好用.
  5. 对于某些方法,我可能只需要返回HttpResponseMessage对象并让调用者处理它.

这大致是我到目前为止所做的,我不知道如何使其符合上述要求我是否正确行事.任何指导都受到赞赏(然而,火焰却没有).

// 200 (+User JSON) = success, otherwise APIError JSON
internal async Task<User> Login (string id, string password)
{
    LoginPayload payload = new LoginPayload() { LoginId = id, Password = password};
    var request = NewRequest(HttpMethod.Post, "login");
    JsonPayload<LoginPayload>(payload, ref request);

    return await Execute<Account>(request, false);
}

// 204: success, anything else failure
internal async Task<Boolean> LogOut ()
{
    return await Execute<Boolean>(NewRequest(HttpMethod.Delete, "login"), true);
}

internal async Task<HttpResponseMessage> GetRawResponse ()
{
    return await Execute<HttpResponseMessage>(NewRequest(HttpMethod.Get, "raw/something"), true);
}

internal async Task<Int32> GetMeStatusCode ()
{
    return await Execute<Int32>(NewRequest(HttpMethod.Get, "some/intstatus"), true);
}

private async Task<RESULT> Execute<RESULT>(HttpRequestMessage request, bool authenticate)
{
    if (authenticate)
        AuthenticateRequest(ref request); // add auth token to request

    var tcs = new TaskCompletionSource<RESULT>();
    var response = await client.SendAsync(request);     

    // TODO: If the RESULT is just HTTPResponseMessage, the rest is unnecessary        

    if (response.IsSuccessStatusCode)
    {
        try
        {
            // TryParse needs to handle Boolean differently than other types
            RESULT result = await TryParse<RESULT>(response);
            tcs.SetResult(result);
        }
        catch (Exception e)
        {
            tcs.SetException(e);
        }

    }
    else
    {
        try
        {
            APIError error = await TryParse<APIError>(response);
            tcs.SetException(new APIException(error));
        }
        catch (Exception e)
        {
            tcs.SetException(new APIException("Unknown error"));
        }
    }

    return tcs.Task.Result;
}
Run Code Online (Sandbox Code Playgroud)

这是APIErrorJSON结构(它是状态代码+自定义错误代码).

{
  "status": 404,
  "code":216,
  "msg":"User not found"
}
Run Code Online (Sandbox Code Playgroud)

我宁愿留下来System.Net,但这主要是因为我不想切换我的所有代码.如果我想要的更容易以其他方式完成,那么显然值得额外的工作.

谢谢.

Sta*_*mos 8

以下是我如何使用MVC API 2作为后端的示例.如果凭据正确,我的后端会返回json结果.UserCredentialsclass是与json结果完全相同的模型.你将不得不使用System.Net.Http.Formatting女巫可以在Microsoft.AspNet.WebApi.ClientNugetPackage中找到

public static async Task<UserCredentials> Login(string username, string password)
{
    string baseAddress = "127.0.0.1/";
    HttpClient client = new HttpClient();

    var authorizationHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes("xyz:secretKey"));
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorizationHeader);



    var form = new Dictionary<string, string>
    {
        { "grant_type", "password" },
        { "username", username },
        { "password", password },
    };

    var Response = await client.PostAsync(baseAddress + "oauth/token", new FormUrlEncodedContent(form));
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        return await Response.Content.ReadAsAsync<UserCredentials>(new[] { new JsonMediaTypeFormatter() });
    }
    else
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

你还需要Newtonsoft.Json包装.

public class UserCredentials
    {
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("expires_in")]
    public int ExpiresIn { get; set; }

    //more properties...
    }
Run Code Online (Sandbox Code Playgroud)


cop*_*lii 1

因此,首先要解决您需要的Newtonsoft.Json评论,我真的还没有感觉到有必要。到目前为止,我发现内置支持运行良好(APIError在我原来的问题中使用 Json:

[DataContract]
internal class APIError
{
    [DataMember (Name = "status")]
    public int StatusCode { get; set; }
    [DataMember (Name = "code")]
    public int ErrorCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我还定义了一个JsonHelper类来(反)序列化:

public class JsonHelper
{
    public static T fromJson<T> (string json)
    {
        var bytes = Encoding.Unicode.GetBytes (json);

        using (MemoryStream mst = new MemoryStream(bytes))
        {
            var serializer = new DataContractJsonSerializer (typeof (T));
            return (T)serializer.ReadObject (mst);
        }
    }

    public static string toJson (object instance)
    {
        using (MemoryStream mst = new MemoryStream())
        {
            var serializer = new DataContractJsonSerializer (instance.GetType());
            serializer.WriteObject (mst, instance);
            mst.Position = 0;

            using (StreamReader r = new StreamReader(mst))
            {
                return r.ReadToEnd();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的部分我已经开始工作了。至于根据预期结果类型处理每个请求执行的单个方法,同时它使我更容易改变处理事物的方式(如错误等),它也增加了代码的复杂性和可读性。我最终创建了单独的方法(Execute原始问题中方法的所有变体:

// execute and return response.StatusCode
private static async Task<HttpStatusCode> ExecuteForStatusCode (HttpRequestMessage request, bool authenticate = true)
// execute and return response without processing
private static async Task<HttpResponseMessage> ExecuteForRawResponse(HttpRequestMessage request, bool authenticate = true)
// execute and return response.IsSuccessStatusCode
private static async Task<Boolean> ExecuteForBoolean (HttpRequestMessage request, bool authenticate = true)
// execute and extract JSON payload from response content and convert to RESULT 
private static async Task<RESULT> Execute<RESULT>(HttpRequestMessage request, bool authenticate = true)
Run Code Online (Sandbox Code Playgroud)

我可以将未经授权的响应(无论如何我当前的代码现在无法处理)转移到一个新方法中,该方法CheckResponse将(例如)在收到 401 时将用户注销。