HttpClient возвращает неполное содержимое ответа

Может ли кто-нибудь объяснить, почему я получаю разные ответы от внешнего HTTP API через Postman и .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;

Не помогло.


person Artem Beziazychnyi    schedule 03.12.2020    source источник
comment
Вы отладили 2 вызова с помощью чего-то вроде скрипачей?   -  person TheGeneral    schedule 03.12.2020
comment
Вы проверили заголовок Content Length ответа? Если он установлен ниже, чем длина тела ответа, вы можете получить такую ​​ситуацию.   -  person Peter Csala    schedule 03.12.2020
comment
заголовок Content-Lenght имеет правильный размер 8192. Если мы загрузим в файл полный ответ JSON, мы увидим, что в нем 8192 символа. Буфер ответа, представляющий собой массив байтов, также имеет длину 8192. Но после var responseContent = await response.Content.ReadAsStringAsync(); responseContent.Lenght = 7596   -  person Artem Beziazychnyi    schedule 03.12.2020
comment
Разве вам не нужно вызывать Result, чтобы получить его? документы. microsoft.com/pt-br/dotnet/api/   -  person Júlio César Schincariol Filho    schedule 03.12.2020
comment
@ JúlioCésarSchincariolFilho alredy пробовал response.Content.ReadAsStringAsync().Result тот же результат   -  person Artem Beziazychnyi    schedule 03.12.2020
comment
Попробуйте JsonSerializer.Deserialize - docs.microsoft.com/en-us/dotnet/standard/serialization/   -  person NishanW    schedule 03.12.2020
comment
откуда вы скопировали первый результат? консоль браузера?   -  person Elyas Esna    schedule 03.12.2020
comment
@ElyasEsna нет, из окна отладки Rider IDE.   -  person Artem Beziazychnyi    schedule 08.12.2020
comment
@ NishānWickramarathna проблема в том, что responseContent недействителен JSON. Итак, фреймворк сериализатора не проблема и работает правильно.   -  person Artem Beziazychnyi    schedule 08.12.2020


Ответы (1)


Проблема была на стороне стороннего API. Владелец API неправильно вычисляет ответ ContentLenght. Алгоритм расчета не основан на кодировке UTF8. Итак, заголовок ContentLenght в ответе отрезает часть тела ответа. Надеюсь, это будет кому-то полезно!

person Artem Beziazychnyi    schedule 26.01.2021