Home > Net >  Consult! HttpClient and upload files to compress the Request
Consult! HttpClient and upload files to compress the Request

Time:11-26

The client (c # Winform. NetFramework 4.8)
(test) nodejs server (express + multer + zlib)

On the premise of not be compressed, can normal to upload files
The client c # code summary
 
String filePath="SOME FILE PATH";

Using (var httpClient=new httpClient ())
{
Using (var form=new MultipartFormDataContent ())
{
Using (var streamContent=new streamContent (File. OpenRead (filePath)))
{
Form. The Add (streamContent, "file", Path. The GetFileName (filePath));
HttpResponseMessage response=httpClient. PostAsync (url, form). The Result;
}
}
}


Nodejs code on the server profile
 
Var storage=multer. MemoryStorage ()
Var upload=multer ({
Storage: storage
})

App. Post ('/compress, upload. Single (' file '), async (the req, res)=& gt; {
Try {
The console. The log (` file: ${JSON. Stringify (the req. File)} `);
.
}
.
}


Then try to Gzip compression
The client c # code summary
- to make a first class compression with the expansion of the
 
Public class GzipCompressedContent: HttpContent
{
Private readonly HttpContent _content;

Public GzipCompressedContent (HttpContent content)
{
//Copy the original headers
The foreach (KeyValuePair The header in the Headers)
{
Headers. TryAddWithoutValidation (header. The Key, the header Value);
}

Headers. ContentEncoding. Add (" gzip ");
_content=content;
}

Protected override async Task SerializeToStreamAsync (Stream, Stream, TransportContext context)
{
Using (var gzip=new GZipStream (stream, CompressionMode.Com press, true))
{
//Compress the - the original content
Await _content. CopyToAsync (gzip);
}
}

Protected override bool TryComputeLength (out long length)
{
//the Content - Lenght is optional, so set to 1
Length=1;
return false;
}
}

- before the API call is compressed
 
String filePath="SOME FILE PATH";

Using (var httpClient=new httpClient ())
{
Using (var form=new MultipartFormDataContent ())
{
Using (var streamContent=new streamContent (File. OpenRead (filePath)))
{
Form. The Add (streamContent, "file", Path. The GetFileName (filePath));

//Gzip compression
Using (var compressed=new GzipCompressedContent (form))
{
HttpResponseMessage response=httpClient. PostAsync (url, compressed). The Result;
}
}
}
}


Server side in the case of not modified, can not receive files, should can't be automatic processing Gzip Request,
So using zlib increased the the following code
 
Var express=the require (' express ');
Var app=express ();

The function gUnzip (the req, res, next) {
Var newReq;
If (the req. Headers [' content - encoding]==='gzip') {
The console. The log (" received gzipped body ");
NewReq=the req. Pipe (zlib. CreateGunzip ());
Object. GetOwnPropertyNames (newReq). ForEach (function (p) {
The req [p]=newReq [p].
});
}
The next ();
}

App. Use (gUnzip);

See the log decompression is run by, but the file still have not received,

Refer to
The practice of c # compression, right?
The client and the server which side has a problem, don't know how to look up,,,

CodePudding user response:

The
reference
the req. Headers [' content - encoding]==='gzip'


See there, he did not set according to the head judge you compression, compression go compression pipe processing pipeline

So, we have not seen in your c # code, you where to set the head

CodePudding user response:

Oh, I see the leak, you added,

So you can use the same file, make the service side to complete a HTTP compression to submit, we take back the packet

Then you are using this file, complete a HTTP submitted, take back the same packet

Then we compare the difference of the two packets to see if his submission and what exactly does our submission is not the same

CodePudding user response:

With the tool is Fiddler, sad is what all have no, in the development environment, and studies how to look under the
C # compression exactly have put the file to the server after all don't know, also don't know which side is front and back side have problem

A Postman, try for the Request is not under compression to the contrast test,,,

Client and server compression this all don't understand, no screening,
Using c # to do a contrast test of a service,,,? The appearance of a little trouble

Hope to have experience of bosses may point out problems in implementation,,,
  •  Tags:  
  • C#
  • Related