les cuento mi problema. Estoy haciendo un Post con HTTPClient con diversos Json. El problema esta es que algunos Json son grandes (de 129 MB por ejemplo), y me tarda mucho para hacer el post (Muchisimo). Intente comprimir el Json con Gzip en el codigo, pero el problema es que el lado que recibe no puede descomprimir el Json Comprimido. Tengo una solucion, pero para eso quiero, saber el tamaño que ocupa la data que hago en el request del client. ¿Que alternativas tengo para descubrir este dato? dejo una version del codigo que estoy usando:
private static HttpResponseMessage Post<T>(string url, string data)
{
Stopwatch stopwatch250K = new Stopwatch();
stopwatch250K.Start();
var clientId = "Client ID";
var clientSecret = "Client Secret";
var bttAPIResource = "bttAPIResource";
var msAuthority = "msAuthority"
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var appCredentials = new ClientCredential(clientId, clientSecret);
var context = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(msAuthority, TokenCache.DefaultShared);
var authResult = context.AcquireTokenAsync(bttAPIResource, appCredentials).Result;
var token = authResult.AccessToken;
var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true, Proxy = CreateWebProxy(true) });
client.DefaultRequestHeaders.Accept.Clear();
client.Timeout = TimeSpan.Parse("00:50:00");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("authorization", $"bearer {token}");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var fullList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(data);
int CurrentPos = 0;
int bufferSizeToUSe = BUFFERSIZE;
if (fullList.Count > BUFFERSIZE)
{
var validateBufferNewResult = fullList.GetRange(0, BUFFERSIZE);
string validateBufferSmallJsonToSend = Newtonsoft.Json.JsonConvert.SerializeObject(validateBufferNewResult);
if (validateBufferSmallJsonToSend.Length > 150000)
{
bufferSizeToUSe = BUFFERSIZE_SMALL;
}
}
while (CurrentPos < fullList.Count)
{
int subListSize = CurrentPos + bufferSizeToUSe > fullList.Count ? fullList.Count - CurrentPos : bufferSizeToUSe;
var newResult = fullList.GetRange(CurrentPos, subListSize);
string smallJsonToSend = Newtonsoft.Json.JsonConvert.SerializeObject(newResult);
var response = client.PostAsync(url, new StringContent(smallJsonToSend, Encoding.UTF8, "application/json")).Result;
if (response.StatusCode == HttpStatusCode.InternalServerError)
{
Console.WriteLine("****result: " + response.StatusCode + " content: " + response.Content.ReadAsStringAsync());
}
Console.WriteLine("result: " + response.StatusCode + " content: " + response.Content.ReadAsStringAsync());
if (stopwatch250K.ElapsedMilliseconds > 600000) // 20 mins
{
client.Dispose();
client = BuildClient();
stopwatch250K.Restart();
}
}
return new HttpResponseMessage(HttpStatusCode.OK);
}