简体   繁体   English

如何使用WebClient C#压缩上传的数据

[英]how to compress uploaded data using WebClient C#

I am using WebClient with C# the following code works fine 我将WebClient与C#结合使用,以下代码可以正常工作

wc = new WebClient();
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
NameValueCollection nvc = new NameValueCollection();
nvc.Add("webdata", JsonConvert.SerializeObject(webdata));
response = wc.UploadValues(@"http://localhost/api/results", "PUT", nvc);

The application is most likely to be used over a mobile data connection, so to minimize costs, i would like to make sure the data is compressed, as it is all txt. 该应用程序最有可能在移动数据连接上使用,因此为了最大程度地降低成本,我想确保数据已压缩,因为它们都是txt。 I have used json instead of xml to reduce the size (and could possibly alter the format to reduce overhead further) 我使用json而不是xml来减小大小(并且可以更改格式以进一步减少开销)

Do i need to compress the data it manually prior to adding it to the WebClient or is there some way i can tell WebClient that my webserver can handle compression (or does compression on the webserver only work for downloads?) 我需要在将数据添加到WebClient之前手动对其进行压缩,还是可以通过某种方式告诉WebClient我的Web服务器可以处理压缩(或者Web服务器上的压缩仅适用于下载吗?)

i am running apache/php on the webserver 我在网络服务器上运行apache / php

thanks in advance 提前致谢

Http compression is normally only used for responses. Http压缩通常仅用于响应。 It is possible to compress requests, but not all web servers will accept these requests and decompress them. 可以压缩请求,但并非所有Web服务器都会接受这些请求并对其进行解压缩。

Have you tried adding a header of type "Content-Encoding" and value "gzip" to your request? 您是否尝试向请求中添加类型为“ Content-Encoding”和值为“ gzip”的标头?

You'll still have to compress the contents manually with a GZipStream and write the compressed bytes out to the request stream though. 您仍然必须使用GZipStream手动压缩内容,然后将压缩后的字节写出到请求流中。

don't forget to flush your writers to the streams, or not all data will be sent over the wire :) 不要忘记将您的编写器刷新到流,否则并非所有数据都将通过网络发送:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM