C
C#8mo ago
Tony Wang

HttpListenerResponse

I am using a .net HttpClient and HttpListener to communicate between two machines. I have trouble configuring the httpListenerResponse correctly. Whatever I tried so far, the HttpReponseMessage that my HttpClient receives always has an empty ContentEncoding header. I need to somehow communicate the encoding of the response back to the client. I could also use the request encoding as a workaround but I would rather avoid that
6 Replies
reflectronic
reflectronic8mo ago
you mean, like, the charset, right? like, UTF-8, ASCII etc.?
Tony Wang
Tony WangOP8mo ago
Yes that's what I mean For the request it works fine but for the response I don't know how to configure it correctly
reflectronic
reflectronic8mo ago
Content-Encoding is not used for the charset that is specified in Content-Type for example, Content-Type: text/html; charset=utf-8 you can set that by using https://learn.microsoft.com/en-us/dotnet/api/system.net.httplistenerresponse.contenttype?view=net-8.0
Tony Wang
Tony WangOP8mo ago
Oh thanks a lot! What is the content encoding header for? So the encoding is automatically set in the string since that string contains byte information as well as encoding information in c#? Unfortunatly it still doesnt work for me. ContentType.CharSet is also empty in the HttpResponseMessage for some reason. Is this something you cannot do with .net HttpListener? So I finally got everything working. I just added: requestResponse.ContentType += ";charset=" + responseWrapper.Encoding?.HeaderName; But what confuses me is setting requestResponse.ContentEncoding doesnt seem to do anything So HttpListenerRequest.ContentEncoding seems to be to be the same as ContentType charset, but HttpListenerResponse.ContentEncoding doesnt do anything it seems
reflectronic
reflectronic8mo ago
yes, as i said the allowed values for Content-Encoding are
Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: br
Content-Encoding: zstd
Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: br
Content-Encoding: zstd
so, it specifies what compression the payload is coded with, not the charset the ContentEncoding property on HttpListenerResponse does nothing, yes
Tony Wang
Tony WangOP8mo ago
I see, it is quite confusing, because the HttpListener parses the CharSet into HttpListenerRequest.ContentEncoding. So suppose I do want to set Content-Encoding to gzip with my HttpListener. How do I do that? Or is there a convention, that the response uses the same compressions as the request and doing anything would be invalid? Thanks again! Finally got it working
Want results from more Discord servers?
Add your server