1

I'm trying to understand whether I'm missing something or whether it's a limitation in HttpWebRequest setting the TransferEncoding value to chunked.

The below code works perfectly fine, as long as I set the SendChunked to true before I set the TransferEncoding value

        var httpWebRequest1 = (System.Net.HttpWebRequest)WebRequest.Create("https://SomeApiHost.com/api/v1");
        httpWebRequest1.SendChunked = true;
        httpWebRequest1.TransferEncoding = "gzip";

however if I attempt the following:

        var httpWebRequest2 = (System.Net.HttpWebRequest)WebRequest.Create("https://SomeApiHost.com/api/v1");
        httpWebRequest2.SendChunked = true;
        httpWebRequest2.TransferEncoding = "Chunked";

I always get this exception

Chunked encoding must be set via the SendChunked property. Parameter name: value

Using HttpClient works fine with the Chunked value, however I can't switch over to it as the RestSharp library uses HttpWebRequest

        var client1 = new System.Net.Http.HttpClient()
        client1.DefaultRequestHeaders.TryAddWithoutValidation("Transfer-Encoding", "chunked");
        client1.DefaultRequestHeaders.TransferEncodingChunked = true;

Does anyone know how to send the Chunked value in TransferEncoding with HttpWebReques?

Domitius
  • 475
  • 6
  • 17
  • If you target .NET Core, you're already using HttpClient. HttpWebRequest is just a legacy wrapper over HttpClient. – Panagiotis Kanavos Nov 15 '21 at 08:37
  • As for the error, it's pretty clear - don't use `TransferEncoding` to set chunked encoding, use `SendChunked`. Why are you using `TransferEncoding = "Chunked"` when you already used `.SendChunked = true;`? The HttpClient code you posted simply writes to the same header twice – Panagiotis Kanavos Nov 15 '21 at 08:42
  • Ah I see what I did there wrong, in order to get RestSharp to set the SendChunked to true you have to pass in a value for TransferEncoding, but testing now and passing in "" or null would actually allows it to be set without having to specify it being "chunked" Thanks – Domitius Nov 15 '21 at 11:45

0 Answers0