简体   繁体   中英

Getting null in httpResponseMessage when calling REST API using HTTPClient

I am trying to execute a REST API which uses HTTP POST. The API consumes and produces xml content. I am getting null in httpResponseMessage and no exception is thrown. I tried executing the same REST API through HttpWebRequest I am getting the response Below you can find Working and Not Working case. Performance wise HttpWebRequest or HTTPClient which is better?

Not Working case HTTPClient

try {
    using(var client = new HttpClient()) {
        Console.WriteLine(inputxml);

        var httpContent = new StringContent(inputxml, Encoding.UTF8, "application/xml");

        Uri testUri = new Uri("http://127.0.0.1:8080/rest/services/getDocument");

        var httpResponseMessage = await client.PostAsync(testUri, httpContent);

        Console.WriteLine(httpResponseMessage.Content.ReadAsStringAsync());

        if (httpResponseMessage.StatusCode == HttpStatusCode.OK) {
            var messageContents = await httpResponseMessage.Content.ReadAsStringAsync();

        }
    }

}

Working Case HTTPWebREquest

 HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://127.0.0.1:8080/rest/services/getDocument");
byte[] bytes = Encoding.UTF8.GetBytes(inputxml.ToString());
request.ContentType = "application/xml";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
HttpWebResponse response;
response = (HttpWebResponse) request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK) {
    Stream responseStream = response.GetResponseStream();
    string responseStr = new StreamReader(responseStream).ReadToEnd();
    Console.WriteLine("responseStr " + responseStr);
    return responseStr;
}
return null;
}

You can use RestSharp for sending HTTP Requests. For me, the best and the simplest method for HTTP Communication.

public string SendRequest(String xml)
    {
        string responseMessage= "";
        RestClient restClient;
        restClient = new RestClient("http://127.0.0.1:8080/rest/services/getDocument");
        RestRequest restRequest = new RestRequest(Method.POST);
        restRequest.AddHeader("Accept", "application/xml");
        restRequest.AddHeader("Content-Type", "application/xml");
        restRequest.AddXmlBody(xml);
        IRestResponse restResponse = restClient.Execute(restRequest);
        if (restResponse.StatusCode == HttpStatusCode.OK)
        {
            responseMessage= restResponse.Content;
        }
        return responseMessage;
    }
}

Here:

Console.WriteLine(httpResponseMessage.Content.ReadAsStringAsync());

You're reading the response body, and printing System.Threading.Task 1` to the console. Then the next time you try to read the response body again, its stream is at its end and won't return anything.

Read the content once, await the call and store the result in a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM