.net - How to compress http request on the fly and without loading compressed buffer in memory -


i need send voluminous data in http post request server supporting gziped encoded requests.

starting simple

public async task<string> dopost(httpcontent content) {   httpclient client = new httpclient();   httpresponsemessage response = await client.postasync("http://myuri", content);    response.ensuresuccessstatuscode();   return await response.content.readasstringasync(); } 

i've added pre compression

public async task<string> dopost(httpcontent content, bool compress) {   if (compress)      content= await compressasync(content);    return await dopost(content); }  private static async task<streamcontent> compressasync(httpcontent content) {   memorystream ms = new memorystream();   using (gzipstream gzipstream = new gzipstream(ms, compressionmode.compress, true))   {     await content.copytoasync(gzipstream);     await gzipstream.flushasync();   }    ms.position = 0;   streamcontent compressedstreamcontent = new streamcontent(ms);   compressedstreamcontent.headers.contenttype = content.headers.contenttype;   compressedstreamcontent.headers.add("content-encoding", "gzip");    return compressedstreamcontent; } 

it works compress data completly loaded memory before sending request. would able compress data on fly during sending in streaming way.

to it, i've tried following code:

private static async task<httpcontent> compressasync2(httpcontent content) {   pushstreamcontent pushstreamcontent = new pushstreamcontent(async (stream, content2, transport) =>   {     using (gzipstream gzipstream = new gzipstream(stream, compressionmode.compress, true))     {       try       {         await content.copytoasync(gzipstream);         await gzipstream.flushasync();       }       catch (exception exception)       {         throw;       }     }   });   pushstreamcontent.headers.contenttype = content.headers.contenttype;   pushstreamcontent.headers.add("content-encoding", "gzip");    return pushstreamcontent; } 

but never goes out of copytoasync(gzipstream). flushasync never executed , no exception thrown , fiddler don't see post started.

my questions are:

  • why compressasync2 doesn't work?
  • how compress on fly during sending , without loading compressed buffer in memory?

any appreciated.

try using compressedcontent class webapicontrib https://github.com/webapicontrib/webapicontrib/blob/master/src/webapicontrib/content/compressedcontent.cs

public async task<string> dopost(httpcontent content) {   httpclient client = new httpclient();   httpresponsemessage response = await client.postasync("http://myuri", new compressedcontent(content,"gzip"));    response.ensuresuccessstatuscode();   return await response.content.readasstringasync(); } 

p.s. stream content on .net 4.5. .net 4 version of httpwebrequest buffers sent content.

p.p.s. creating new httpclient each request not best way use httpclient. doing force new tcp connection created each request.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -