在Http响应消息中找到POST的结果

时间:2019-02-10 03:59:10

标签: c# rest httpresponse

我正在VS2017中使用Windows窗体应用程序构建REST Client(c#)。我已经能够使用HttpClient.PostAsJsonAsync()和ReadAsync()方法成功地从服务器获取和发布请求。我正在尝试将一些数据发布到服务器,并且在成功完成POST之后,服务器将使用诸如“ 1c77ad2e-54e0-4187-aa9d-9a55286b1f7a”

的唯一字符串进行响应

我为POST获得成功的响应代码(200-OK)。但是,我不确定在哪里检查结果字符串。我已经检查了HttpResponseMessage.Content的内容

static async Task<HttpStatusCode> PostBurstData(string path, BurstRequest 
burstRequest)
    {
        HttpResponseMessage response = await client.PostAsJsonAsync(path, burstRequest);

        response.EnsureSuccessStatusCode();
        Console.WriteLine(response.Content.ToString());

        // return response
        return response.StatusCode;
    }

发送到此函数调用的数据如下:

BurstRequest request = new BurstRequest();
            request.NodeSerialNumbers = SubSerialList;
            request.StartTime = ((DateTimeOffset)dateTimePicker1.Value).ToUnixTimeMilliseconds();

            HttpStatusCode statusCode = new HttpStatusCode();

            statusCode = await PostBurstData(post_burst_url, request);

我应该在哪里搜索服务器响应成功的POST的字符串?是否应该使用ReadAsync()读取内容?

if (response.IsSuccessStatusCode)
        {
            var data = await response.Content.ReadAsAsync();

        } 

1 个答案:

答案 0 :(得分:0)

为什么不尝试使用HttpContent呢?

using (HttpResponseMessage response = await client.GetAsync(url)) // here you can use your own implementation i.e PostAsJsonAsync
        {
            using (HttpContent content = response.Content)
            {
                string responseFromServer = await content.ReadAsStringAsync();

            }

        }