Last post Jun 11, 2020 05:51 PM by bruce (sqlwork.com)
Jun 11, 2020 12:14 PM|wanttolearn1|LINK
I am trying to get an image size from a web site using HttpClient on Core Ver2.0.
I am doing :
private static readonly string browserVer = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36";
HttpResponseMessage response = await httpClient.gets(url);
but when i check the Content-Length header but it not exist.
should i add some settings for the request.
Jun 11, 2020 12:59 PM|mgebhard|LINK
Did the request return anything? What is gets()? Are there any exceptions? Try setting a break point and stepping through the code.
Jun 11, 2020 02:34 PM|bruce (sqlwork.com)|LINK
Sending a content length header is optional. Often you need to read the body stream to get the length.
Jun 11, 2020 03:07 PM|wanttolearn1|LINK
it was under response content
Jun 11, 2020 03:16 PM|bruce (sqlwork.com)|LINK
yes, but the sender of the response, is not required to send the length. They can use a FIN instead.
Jun 11, 2020 03:42 PM|wanttolearn1|LINK
what do you mean by FIN?
Jun 11, 2020 04:43 PM|mgebhard|LINK
FIN is a packet used to terminate a TCP connection.
If you are sending messages to an web serve then it should return the content-length header. There's no guarantee. As recommended above, do a GET for the image and read the response byte length.
Otherwise, send an HTTP HEAD which returns the same HTTP header as a GET but without the content. Below is a non-production working example.
using (var client = new HttpClient())
HttpRequestMessage message = new HttpRequestMessage();
message.Method = HttpMethod.Head;
message.RequestUri = new Uri("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
HttpResponseMessage response = await client.SendAsync(message);
foreach(var h in response.Content.Headers)
Expires: Thu, 11 Jun 2020 16:42:39 GMT
Last-Modified: Tue, 22 Oct 2019 18:30:00 GMT
Jun 11, 2020 05:51 PM|bruce (sqlwork.com)|LINK
but then a HEAD may not return the length either. It is useful for static files, but when calling dynamic content, it often ends up running all the code, then throwing away the content.