当前位置: 首页 > 知识库问答 >
问题:

HttpClient返回不完整的响应内容

子车成和
2023-03-14

有人能解释一下为什么我通过Postman和http从外部http API得到不同的响应吗。NetCore web Api HttpClient。

这是一个密码

string westLakeApiUrl = "https://external.api.example";

HttpClient client = new HttpClient();

HttpContent content = new StringContent("");
content.Headers.ContentType = new MediaTypeHeaderValue("text/json");

var response = await client.PostAsync(westLakeApiUrl, content);
var responseContent = await response.Content.ReadAsStringAsync();

dynamic data = JsonConvert.DeserializeObject(responseContent);
return new OkObjectResult(data);

结果是

{"totalRecords":86,"requestSucceeded":true,"countryList":[{"countryISOCode":"CW","countryName":"Curaçao","localCountryName":"Curaçao","countryId":77},{"countryISOCode":"CZ","countryName":"Czech/Slovak Republic","localCountryName":"Czech/Slovak Republic","countryId":44},{"countryISOCode":"GR","countryName":"Greece/Cyprus","localCountryName":"Greece/Cyprus","countryId":27},{"countryISOCode":"AE","countryName":"Gulf","localCountryName":"Gulf","countryId":32},{"countryISOCode":"EA","countryName":"SE Asia","localCountryName":"SE Asia","countryId":61},{"countryISOCode":"RS","countryName":"Serbia","localCountryName":"Serbia","countryId":87},{"countryISOCode":"UK","countryName":"United Kingdom","localCountryName":"UK","countryId":15},{"countryISOCode":"AU","countryName":"Australia","localCountryName":"Австралия","countryId":19},{"countryISOCode":"AT","countryName":"Austria","localCountryName":"Австрия","countryId":22},{"countryISOCode":"AZ","countryName":"Azerbaijan","localCountryName":"Азербайджан","countryId":72},{"countryISOCode":"AR","countryName":"Argentina","localCountryName":"Аргентина","countryId":60},{"countryISOCode":"AM","countryName":"Armenia","localCountryName":"Армения","countryId":71},{"countryISOCode":"BD","countryName":"Bangladesh","localCountryName":"Бангладеш","countryId":73},{"countryISOCode":"BY","countryName":"Belarus","localCountryName":"Беларусь","countryId":74},{"countryISOCode":"BE","countryName":"Belgium","localCountryName":"Бельгия","countryId":6},{"countryISOCode":"BG","countryName":"Bulgaria","localCountryName":"Болгария","countryId":75},{"countryISOCode":"BO","countryName":"Bolivia","localCountryName":"Боливия","countryId":95},{"countryISOCode":"BR","countryName":"Brazil","localCountryName":"Бразилия","countryId":49},{"countryISOCode":"HU","countryName":"Hungary","localCountryName":"Венгрия","countryId":58},{"countryISOCode":"VE","countryName":"Venezuela","localCountryName":"Венесуэла","countryId":92},{"countryISOCode":"VN","countryName":"Vietnam","localCountryName":"Вьетнам","countryId":39},{"countryISOCode":"GT","countryName":"Guatemala","localCountryName":"Гватемала","countryId":63},{"countryISOCode":"DE","countryName":"Germany","localCountryName":"Германия","countryId":16},{"countryISOCode":"HN","countryName":"Honduras","localCountryName":"Гондурас","countryId":79},{"countryISOCode":"HK","countryName":"Hong Kong","localCountryName":"Гонконг","countryId":28},{"countryISOCode":"DK","countryName":"Denmark","localCountryName":"Дания","countryId":21},{"countryISOCode":"DO","countryName":"Republica Dominicana","localCountryName":"Доминиканская Республика","countryId":67},{"countryISOCode":"EG","countryName":"Egypt","localCountryName":"Египт","countryId":35},{"countryISOCode":"IL","countryName":"Israel","localCountryName":"Израиль","countryId":36},{"countryISOCode":"IN","countryName":"India","localCountryName":"Индия","countryId":31},{"countryISOCode":"ID","countryName":"Indonesia","localCountryName":"Индонезия","countryId":41},{"countryISOCode":"IE","countryName":"Ireland","localCountryName":"Ирландия","countryId":24},{"countryISOCode":"ES","countryName":"Spain","localCountryName":"Испания","countryId":46},{"countryISOCode":"IT","countryName":"Italy","localCountryName":"Италия","countryId":51},{"countryISOCode":"KZ","countryName":"Kazakhstan","localCountryName":"Казахстан","countryId":80},{"countryISOCode":"CA","countryName":"Canada","localCountryName":"Канада","countryId":12},{"countryISOCode":"KG","countryName":"Kyrgyzstan","localCountryName":"Киргизстан","countryId":81},{"countryISOCode":"CN","countryName":"China","localCountryName":"Китай","countryId":42},{"countryISOCode":"CO","countryName":"Colombia","localCountryName":"Колумбия","countryId":52},{"countryISOCode":"CR","countryName":"Costa Rica","localCountryName":"Коста-Рика","countryId":68},{"countryISOCode":"LV","countryName":"Latvia","localCountryName":"Латвия","countryId":82},{"countryISOCode":"LT","countryName":"Lithuania","localCountryName":"Литва","countryId":83},{"countryISOCode":"MY","countryName":"Malaysia","localCountryName":"Малайзия","countryId":30},{"countryISOCode":"MX","countryName":"Mexico","localCountryName":"Мексика","countryId":4},{"countryISOCode":"MN","countryName":"Mongolia","localCountryName":"Монголия","countryId":84},{"countryISOCode":"NP","countryName":"Nepal","localCountryName":"Непал","countryId":85},{"countryISOCode":"NL","countryName":"Netherlands","localCountryName":"Нидерланды","countryId":10},{"countryISOCode":"NI","countryName":"Nicaragua","localCountryName":"Никарагуа","countryId":86},{"countryISOCode":"NZ","countryName":"New Zealand","localCountryName":"Новая Зеландия","countryId":14},{"countryISOCode":"NO","countryName":"Norway","localCountryName":"Норвегия","countryId":25},{"countryISOCode":"PA","countryName":"Panama","localCountryName":"Панама","countryId":64},{"countryISOCode":"PY","countryName":"Paraguay","localCountryName":"Парагвай","countryId":65},{"countryISOCode":"PE","countryName":"Peru","localCountryName":"Перу","countryId":55},{"countryISOCode":"PL","countryName":"Poland","localCountryName":"Польша","countryId":43},{"countryISOCode":"PT","countryName":"Portugal","localCountryName":"Португалия","countryId":57},{"countryISOCode":"PR","countryName":"Puerto Rico","localCountryName":"Пуэрто-Рико","countryId":66},{"countryISOCode":"RU","countryName":"Russia","localCountryName":"Россия","countryId":48},{"countryISOCode":"RO","countryName":"Romania","localCountryName":"Румыния","countryId":45},{"countryISOCode":"SV","countryName":"El Salvador","localCountryName":"Сальвадор","countryId":78},{"countryISOCode":"SA","countryName":"Saudi Arabia","localCountryName":"Саудовская Аравия","countryId":33},{"countryISOCode":"SG","countryName":"Singapore","localCountryName":"Сингапур","countryId":11},{"countryISOCode":"SI","countryName":"Slovenia","localCountryName":"Словения","countryId":93},{"countryISOCode":"US","countryName":"United States","localCountryName":"Соединенные Штаты","countryId":17},{"countryISOCode":"TJ","countryName":"Tajikistan","localCountryName":"Таджикистан","countryId":89},{"countryISOCode":"TH","countryName":"Thailand","localCountryName":"Таиланд","countryId":38},{"countryISOCode":"TW","countryName":"Taiwan","localCountryName":"Тайвань","countryId":29},{"countryISOCode":"TT","countryName":"Trinidad y Tobago","localCountryName":"Тринидад и Тобаго","countryId":70},{"countryISOCode":"TM","countryName":"Turkmenistan","localCountryName":"Туркменистан","countryId":90},{"countryISOCode":"TR","countryName":"Turkey","localCountryName":"Турция","countryId":34},{"countryISOCode":"UZ","countryName":"Uzbekistan","localCountryName":"Узбекистан","countryId":91},{"countryISOCode":"UA","countryName":"Ukraine","localCountryName":"Украина","countryId":47},{"countryISOCode":"UY","countryName":"Uruguay","localCountryName":"Уругвай","countryId":94},{"countryISOCode":"PH","countryName":"Philippines","localCountryName":"Филиппины","countryId":40},{"countryISOCode":"FI","countryName":"Finland","localCountryName":"Финляндия","countryId":3},{"countryISOCode":"FR","countryName":"France","localCountryName":"Франция","countryId":26},{"countryISOCode":"HR","countryName":"Croatia","localCountryName":"Хорватия","countryId":76},{"countryISOCode":"CL","countryName":"Chile","localCountryName":"Чили","countryId":56},{"countryISOCode":"CH","countryName":"Switzerland","localCountryName":"Швейцария","countryId":18},{"countryISOCode":"SE","countryName":"Sweden","localCountryName":"Швеция","countryId":9},{"countryISOCode":"LK","countryName":"Sri Lanka","localCountr

但Postman返回完整有效的json结果。

{"totalRecords":86,"requestSucceeded":true,"countryList":[{"countryISOCode":"CW","countryName":"Curaçao","localCountryName":"Curaçao","countryId":77},{"countryISOCode":"CZ","countryName":"Czech/Slovak Republic","localCountryName":"Czech/Slovak Republic","countryId":44},{"countryISOCode":"GR","countryName":"Greece/Cyprus","localCountryName":"Greece/Cyprus","countryId":27},{"countryISOCode":"AE","countryName":"Gulf","localCountryName":"Gulf","countryId":32},{"countryISOCode":"EA","countryName":"SE Asia","localCountryName":"SE Asia","countryId":61},{"countryISOCode":"RS","countryName":"Serbia","localCountryName":"Serbia","countryId":87},{"countryISOCode":"UK","countryName":"United Kingdom","localCountryName":"UK","countryId":15},{"countryISOCode":"AU","countryName":"Australia","localCountryName":"Австралия","countryId":19},{"countryISOCode":"AT","countryName":"Austria","localCountryName":"Австрия","countryId":22},{"countryISOCode":"AZ","countryName":"Azerbaijan","localCountryName":"Азербайджан","countryId":72},{"countryISOCode":"AR","countryName":"Argentina","localCountryName":"Аргентина","countryId":60},{"countryISOCode":"AM","countryName":"Armenia","localCountryName":"Армения","countryId":71},{"countryISOCode":"BD","countryName":"Bangladesh","localCountryName":"Бангладеш","countryId":73},{"countryISOCode":"BY","countryName":"Belarus","localCountryName":"Беларусь","countryId":74},{"countryISOCode":"BE","countryName":"Belgium","localCountryName":"Бельгия","countryId":6},{"countryISOCode":"BG","countryName":"Bulgaria","localCountryName":"Болгария","countryId":75},{"countryISOCode":"BO","countryName":"Bolivia","localCountryName":"Боливия","countryId":95},{"countryISOCode":"BR","countryName":"Brazil","localCountryName":"Бразилия","countryId":49},{"countryISOCode":"HU","countryName":"Hungary","localCountryName":"Венгрия","countryId":58},{"countryISOCode":"VE","countryName":"Venezuela","localCountryName":"Венесуэла","countryId":92},{"countryISOCode":"VN","countryName":"Vietnam","localCountryName":"Вьетнам","countryId":39},{"countryISOCode":"GT","countryName":"Guatemala","localCountryName":"Гватемала","countryId":63},{"countryISOCode":"DE","countryName":"Germany","localCountryName":"Германия","countryId":16},{"countryISOCode":"HN","countryName":"Honduras","localCountryName":"Гондурас","countryId":79},{"countryISOCode":"HK","countryName":"Hong Kong","localCountryName":"Гонконг","countryId":28},{"countryISOCode":"DK","countryName":"Denmark","localCountryName":"Дания","countryId":21},{"countryISOCode":"DO","countryName":"Republica Dominicana","localCountryName":"Доминиканская Республика","countryId":67},{"countryISOCode":"EG","countryName":"Egypt","localCountryName":"Египт","countryId":35},{"countryISOCode":"IL","countryName":"Israel","localCountryName":"Израиль","countryId":36},{"countryISOCode":"IN","countryName":"India","localCountryName":"Индия","countryId":31},{"countryISOCode":"ID","countryName":"Indonesia","localCountryName":"Индонезия","countryId":41},{"countryISOCode":"IE","countryName":"Ireland","localCountryName":"Ирландия","countryId":24},{"countryISOCode":"ES","countryName":"Spain","localCountryName":"Испания","countryId":46},{"countryISOCode":"IT","countryName":"Italy","localCountryName":"Италия","countryId":51},{"countryISOCode":"KZ","countryName":"Kazakhstan","localCountryName":"Казахстан","countryId":80},{"countryISOCode":"CA","countryName":"Canada","localCountryName":"Канада","countryId":12},{"countryISOCode":"KG","countryName":"Kyrgyzstan","localCountryName":"Киргизстан","countryId":81},{"countryISOCode":"CN","countryName":"China","localCountryName":"Китай","countryId":42},{"countryISOCode":"CO","countryName":"Colombia","localCountryName":"Колумбия","countryId":52},{"countryISOCode":"CR","countryName":"Costa Rica","localCountryName":"Коста-Рика","countryId":68},{"countryISOCode":"LV","countryName":"Latvia","localCountryName":"Латвия","countryId":82},{"countryISOCode":"LT","countryName":"Lithuania","localCountryName":"Литва","countryId":83},{"countryISOCode":"MY","countryName":"Malaysia","localCountryName":"Малайзия","countryId":30},{"countryISOCode":"MX","countryName":"Mexico","localCountryName":"Мексика","countryId":4},{"countryISOCode":"MN","countryName":"Mongolia","localCountryName":"Монголия","countryId":84},{"countryISOCode":"NP","countryName":"Nepal","localCountryName":"Непал","countryId":85},{"countryISOCode":"NL","countryName":"Netherlands","localCountryName":"Нидерланды","countryId":10},{"countryISOCode":"NI","countryName":"Nicaragua","localCountryName":"Никарагуа","countryId":86},{"countryISOCode":"NZ","countryName":"New Zealand","localCountryName":"Новая Зеландия","countryId":14},{"countryISOCode":"NO","countryName":"Norway","localCountryName":"Норвегия","countryId":25},{"countryISOCode":"PA","countryName":"Panama","localCountryName":"Панама","countryId":64},{"countryISOCode":"PY","countryName":"Paraguay","localCountryName":"Парагвай","countryId":65},{"countryISOCode":"PE","countryName":"Peru","localCountryName":"Перу","countryId":55},{"countryISOCode":"PL","countryName":"Poland","localCountryName":"Польша","countryId":43},{"countryISOCode":"PT","countryName":"Portugal","localCountryName":"Португалия","countryId":57},{"countryISOCode":"PR","countryName":"Puerto Rico","localCountryName":"Пуэрто-Рико","countryId":66},{"countryISOCode":"RU","countryName":"Russia","localCountryName":"Россия","countryId":48},{"countryISOCode":"RO","countryName":"Romania","localCountryName":"Румыния","countryId":45},{"countryISOCode":"SV","countryName":"El Salvador","localCountryName":"Сальвадор","countryId":78},{"countryISOCode":"SA","countryName":"Saudi Arabia","localCountryName":"Саудовская Аравия","countryId":33},{"countryISOCode":"SG","countryName":"Singapore","localCountryName":"Сингапур","countryId":11},{"countryISOCode":"SI","countryName":"Slovenia","localCountryName":"Словения","countryId":93},{"countryISOCode":"US","countryName":"United States","localCountryName":"Соединенные Штаты","countryId":17},{"countryISOCode":"TJ","countryName":"Tajikistan","localCountryName":"Таджикистан","countryId":89},{"countryISOCode":"TH","countryName":"Thailand","localCountryName":"Таиланд","countryId":38},{"countryISOCode":"TW","countryName":"Taiwan","localCountryName":"Тайвань","countryId":29},{"countryISOCode":"TT","countryName":"Trinidad y Tobago","localCountryName":"Тринидад и Тобаго","countryId":70},{"countryISOCode":"TM","countryName":"Turkmenistan","localCountryName":"Туркменистан","countryId":90},{"countryISOCode":"TR","countryName":"Turkey","localCountryName":"Турция","countryId":34},{"countryISOCode":"UZ","countryName":"Uzbekistan","localCountryName":"Узбекистан","countryId":91},{"countryISOCode":"UA","countryName":"Ukraine","localCountryName":"Украина","countryId":47},{"countryISOCode":"UY","countryName":"Uruguay","localCountryName":"Уругвай","countryId":94},{"countryISOCode":"PH","countryName":"Philippines","localCountryName":"Филиппины","countryId":40},{"countryISOCode":"FI","countryName":"Finland","localCountryName":"Финляндия","countryId":3},{"countryISOCode":"FR","countryName":"France","localCountryName":"Франция","countryId":26},{"countryISOCode":"HR","countryName":"Croatia","localCountryName":"Хорватия","countryId":76},{"countryISOCode":"CL","countryName":"Chile","localCountryName":"Чили","countryId":56},{"countryISOCode":"CH","countryName":"Switzerland","localCountryName":"Швейцария","countryId":18},{"countryISOCode":"SE","countryName":"Sweden","localCountryName":"Швеция","countryId":9},{"countryISOCode":"LK","countryName":"Sri Lanka","localCountryName":"Шри-Ланка","countryId":88},{"countryISOCode":"EC","countryName":"Ecuador","localCountryName":"Эквадор","countryId":62},{"countryISOCode":"EE","countryName":"Estonia","localCountryName":"Эстония","countryId":96},{"countryISOCode":"ZA","countryName":"South Africa","localCountryName":"Южная Африка","countryId":37},{"countryISOCode":"KR","countryName":"Korea","localCountryName":"Южная Корея","countryId":23},{"countryISOCode":"JM","countryName":"Jamaica","localCountryName":"Ямайка","countryId":69},{"countryISOCode":"JP","countryName":"Japan","localCountryName":"Япония","countryId":20}]}

为什么结果从。NetCore HttpClient是部分的?

我已经尝试了指定请求头这样的选项:

content.Headers.ContentType.CharSet = "UTF-8";
content.Headers.Add("Strict-Transport-Security", new string[]{"max-age=63072000", "includeSubdomains"});
content.Headers.Add("Access-Control-Allow-Origin", new string[]{"*"});
content.Headers.Add("Access-Control-Allow-Methods", new string[]{"POST", "GET"});
content.Headers.Add("Access-Control-Allow-Headers", new string[]{"accountId","accessKey","Content-Type","Content-Length","Accept,Origin","Referer","User-Agent"});
content.Headers.Add("Access-Control-Max-Age", new string[]{"86400"});
content.Headers.Add("Keep-Alive", new string[]{"timeout=5, max=100"});
content.Headers.ContentLength = 0;

还补充道:

client.DefaultRequestHeaders.ExpectContinue = false;

没有帮助。

共有1个答案

逑禄
2023-03-14

问题出在第三方API方面。API错误的所有者计算响应ContentLenght。计算算法不是基于UTF8编码的。因此,响应中的ContentLenght标头会削减响应主体的一部分。希望对某人有帮助!

 类似资料:
  • 问题内容: 我正在使用Node的模块发出 HTTP 请求,但是在上,返回的块似乎并不满足完整的请求响应。这是我的代码: 有没有办法在结束请求之前等待完整的输出?难道我做错了什么?谢谢! 问题答案: 您还应该收听“结束”事件

  • 我没有得到实际的超文本传输协议标头时,使超文本传输协议后调用使用角5公共http pClient.我在请求中传递观察:'响应'以获得完整的响应(默认情况下,http客户端仅返回响应中的正文)。 我想从http头响应中读取csrftoken值。我可以看到csrftoken在使用chrome浏览器网络视图的响应头中可用。 但是当我在Angular中阅读httpClient响应时,这一点是不可用的。 在

  • 当我使用Guzzle发送请求时,它会返回一条完整的错误消息,而不会解析内部的JSON。下面是它的外观: “”“ 客户端错误: 导致 响应:\n {\r\n ”错误“: [\r\n ”潜在客户已在此市场活动中“\r\n ]\r\n }\n ”“” 当我通过Postman发送相同的请求时,它确实会返回正确解析的响应消息。 我如何才能让Guzzle只返回消息,而不是完整的响应?

  • 使用Guzzle,我正在使用JSON格式的一些外部API,通常我获取数据 $data = $request- 但是我无法从这个不同的api中获取数据。数据似乎没有出现在“响应体”中。 这个api调用有效:https://i.ibb.co/80Yk6dx/Screenshot-2.png 这不起作用:https://i.ibb.co/C239ghy/Screenshot-3.png

  • 我在调用一个SOAP API。PowerShell给出了一个错误,这个错误包含了我所期望的响应!而我发送响应的变量的内容缺少一些数据。 为空! SOAPUI给出了正确的错误:

  • 123 但是我想要像这样的整个字符串 编辑:添加了,但响应相同。