HttpClient截断响应内容

时间:2017-08-21 09:13:50

标签: c# xml console-application dotnet-httpclient

我使用httpclient做了一些请求。一个使用Content-Type:application/json,另一个使用Content-Type:application/xml

这是我的代码:

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage();
request.Method = HttpMethod.Post;
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
request.Content = new StringContent(BodyContent, Encoding.UTF8, "application/xml");

client.BaseAddress = new Uri(baseUrl);
var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead);
var responseText = await response.Content.ReadAsStringAsync();
Result = responseText.ToString();
client.Dispose();
request.Dispose();

当内容类型为json时,此代码可以正常工作,但有时在内容类型xml时返回截断的响应内容。在我的情况下,返回json的字符串内容超过xml

此处回复example.xml

的示例

你知道为什么会这样吗?

0 个答案:

没有答案