简体   繁体   English

从HttpClient到用户定义的对象C#的HttpResponseMessage

[英]HttpResponseMessage from HttpClient to User Defined Object C#

I have seen similar questions asked but none that seem to help me with my issue so please bear with me. 我曾问过类似的问题,但似乎没有一个问题对我有帮助,所以请耐心等待。

I have a WebAPI controller method that is defined as such: 我有一个这样定义的WebAPI控制器方法:

[HttpPost]
[Route("")]
public HttpResponseMessage CreateMyObject(MyObjectRequest myObject)
{
    MyObject o;
    try
    {
        o = _serviceFactory.GetInstance().CreateMyObject(myObject);
    }
    catch (Exception ex)
    {
        ex.WriteToLog();
        throw ApiHelper.CreateResponseException(HttpStatusCode.InternalServerError, ex);
    }
    var response = Request.CreateResponse(HttpStatusCode.Created, o);
    var uri = Url.Link("GetMyObjectById", new { myObjectId = o.MyObjectId.ToString() });
    response.Headers.Location = new Uri(uri);
    return response;
}

Say, MyObject contains two properties, 假设MyObject包含两个属性,

public MyObject
{
    public Guid MyObjectId;
    public string MyObjectName
}

A client was written to call these controller methods in a WPF application. 编写了一个客户端以在WPF应用程序中调用这些控制器方法。 Here is the client method that is being used: 这是正在使用的客户端方法:

public HttpResponseMessage CreateQuote(MyObjectRequest myObject)
{
    var hashtable = new Hashtable
                {
                    {"myObject", myObject}
                };

    var task = GetResponse("", hashtable);
    var response = task.Result;

    return response;
} 

protected async Task<HttpResponseMessage> GetResponse(string path, Hashtable parameters)
{
    var response = await GetAsync(BuildRequestUri(path, parameters)).ConfigureAwait(false);
    return response.IsSuccessStatusCode ? response : new HttpResponseMessage();
}

protected async Task<HttpResponseMessage> GetResponse(string path)
{
    return await GetResponse(path, null);
}

The controller and supporting client code was not written by me and was already in the system. 控制器和支持的客户端代码不是我写的,并且已经在系统中。 I am just consuming this in the WPF application. 我只是在WPF应用程序中使用它。 Now, I am trying to call the controller method via the client in the application and get the MyObject from the response so that I can access the MyObjectId that has been created and set. 现在,我试图通过应用程序中的客户端调用控制器方法,并从响应中获取MyObject,以便可以访问已创建和设置的MyObjectId。 I have tried some of the other responses to similar questions but have not even seen some of the methods that are called on the response in order to get the information. 我已经尝试了对类似问题的其他一些响应,但是甚至没有看到为了获得信息而在响应中调用的一些方法。 Here is the first part of the call to the client that I have in the application: 这是我在应用程序中对客户端的调用的第一部分:

var httpResponse = ApplicationService.CreateMyObject(myObjectRequest);

The application service simply injects the client into the constructor and allows me to call the CreateMyObject method. 应用程序服务只是将客户端注入到构造函数中,并允许我调用CreateMyObject方法。 Is there any insight that can be given to me on how I should be getting the MyObject object out of the response? 对于应该如何从响应中获取MyObject对象,是否可以提供任何见解?

I'm still a little new to web api as well, but I'm currently working with it on a project. 我对Web api还是有点陌生​​,但是我目前正在一个项目中使用它。 Give the following code a try: 尝试以下代码:

MyObject myObject;
if (response.IsSuccessStatusCode)
{
    // Parse the response body. Blocking!
    myObject = response.Content.ReadAsAsync<MyObject>().Result;
}

So you could theoretically change your method like this (may not be exactly what you want): 因此,理论上您可以像这样更改您的方法(可能不完全是您想要的):

public MyObject CreateQuote(MyObjectRequest myObject)
{
    var hashtable = new Hashtable
                {
                    {"myObject", myObject}
                };

    var task = GetResponse("", hashtable);
    var response = task.Result;

    MyObject newObject;
    if (response.IsSuccessStatusCode)
    {
        // Parse the response body. Blocking!
        newObject= response.Content.ReadAsAsync<MyObject>().Result;
    }

    return newObject; // instead of response
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 将 HttpResponseMessage 反序列化为对象 - C# Deserializing HttpResponseMessage to Object C#从HttpResponseMessage读取 - C# read from HttpResponseMessage HttpResponseMessage是IsSuccessStatusCode C# - HttpResponseMessage is IsSuccessStatusCode C# C#中的Httpwebresponse对Httpresponsemessage - Httpwebresponse to Httpresponsemessage in c# C#HttpResponseMessage.Content没有将MemoryStream正确返回给HttpClient,而是使用byte []进行返回工作 - c# HttpResponseMessage.Content does not return MemoryStream correctly to HttpClient but using byte[] for return works 如何将具有OData的HttpResponseMessage转换为C#对象? - How to convert HttpResponseMessage having OData to a C# object? Output 从 HttpClient HttpResponseMessage 到控制台的所有标头 - Output all headers from HttpClient HttpResponseMessage to console c# 为 HttpClient 从 model object 构建 URL 编码查询 - c# Build URL encoded query from model object for HttpClient C#使用HttpClient从JSON响应中获取特定对象 - C# Get specific object from JSON response using HttpClient 从HttpResponseMessage获取内容以使用c#动态关键字进行测试 - Getting content from HttpResponseMessage for testing using c# dynamic keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM