[英]Getting null in httpResponseMessage when calling REST API using HTTPClient
I am trying to execute a REST API which uses HTTP POST.我正在尝试执行使用 HTTP POST 的 REST API。 The API consumes and produces xml content. API 消耗并产生 xml 内容。 I am getting null in httpResponseMessage and no exception is thrown.我在 httpResponseMessage 中得到 null 并且没有抛出异常。 I tried executing the same REST API through HttpWebRequest I am getting the response Below you can find Working and Not Working case.我尝试通过 HttpWebRequest 执行相同的 REST API 我得到了响应下面你可以找到工作和不工作的情况。 Performance wise HttpWebRequest or HTTPClient which is better?性能明智的 HttpWebRequest 或 HTTPClient 哪个更好?
Not Working case HTTPClient不工作的情况下 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
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.您可以使用 RestSharp 发送 HTTP 请求。 For me, the best and the simplest method for HTTP Communication.对我来说,HTTP 通信的最佳和最简单的方法。
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.您正在阅读响应正文,并将System.Threading.Task
1` 打印到控制台。 Then the next time you try to read the response body again, its stream is at its end and won't return anything.然后下次您尝试再次读取响应正文时,它的 stream 已结束,不会返回任何内容。
Read the content once, await the call and store the result in a variable.读取内容一次,等待调用并将结果存储在变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.