0

This is my method, an async HTTP POST :

using System;                               //main data types
using System.Net.Http;                      //for HTTP client
using System.Threading.Tasks;               //for Async Request/Response
using Newtonsoft.Json;                      //for JSON properties

//... the call..
public void Something()
{
PostConsentAsync(cnsnt.BaseURL, cnsnt.Headers, cnsnt.Body).GetAwaiter().GetResult();
}

//... the async task...
public async Task HttpPostAsync(Uri HTTPaddress, cHeaders myHeaders, cBody myBody)
{
    try
    {
        HttpClient client = new HttpClient();
        System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, HTTPaddress))
        {
            client.BaseAddress = HTTPaddress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));

            client.DefaultRequestHeaders.Add("Connection", "keep-alive");
            client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
            client.DefaultRequestHeaders.Add("SomeHeader", myHeaders.SomeHeader);
            client.DefaultRequestHeaders.Add("MyOtherHeader", myHeaders.MyOtherHeader);
            //etc..

            request.Content = new StringContent(JsonConvert.SerializeObject(myBody, Formatting.Indented), utf8, "application/json");

            using (HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false))
            {
                Int32 code = (Int32)response.StatusCode;

                //How to read ALL the response, including headers, body etc???

                Console.WriteLine("03\r\nRESPONSE: {0}", response.RequestMessage.ToString() + "\r\nBody:\r\n" + <?????> + "Response HTTP status: " + code + " [" + response.StatusCode.ToString() + "]");
                Console.ReadLine();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error in " + e.TargetSite + "\r\n" + e.Message); Console.ReadLine();
    }
}

I need to read all the response, but seems difficult... I need to have the whole response message and, in addition, in some form like response. so that I can feed this to another HTTP request. So I was trying to get the whole response and somehow feed it in a class and have something like response.myHeader1.Name (name of header) or response.myHeader1.Value (value of that header, null if null) etc, or something like this. But I can't get the whole message, I get the headers, I get the code (i.e. 200 is success etc) but the body is not there etc.

1 Answer 1

1

In order to retrieve the body, you need to access the Content property of HttpResponseMessage, for example:

var body = await response.Content.ReadAsStringAsync();

Sign up to request clarification or add additional context in comments.

3 Comments

The OP is asking how to get the raw response returned by the server, not just the content
@Pedro: Excellent, indeed body is retrieved. By itself is a significant leap forward for me. However, I would need the whole thing, some headers have values that I need to read (among other things, hopefully all the info in the response message from the server) Thank you for your insight!
oh, I misread that you were able to get the Headers and Status code already and only the body was missing. Pulling them out of response.Headers and response.StatusCode is not enough?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.