简体   繁体   中英

Compress data sent to Webservice

我正在将XMLDocument发送到我的Web服务(.asmx),当我通过xmldocument.outerxml.tostring()。length检查该xmldocumetn的文件大小时,它大约是433434(我不知道它代表什么...是吗?所以我想从客户端压缩此数据并将其发送到webservice而不将文件另存为XML在我的本地硬盘驱动器中。

xmldocument.OuterXml.ToString().Length will return the length of the xml string, like "something".Length will return 9. So no, it is not in bytes.

The actual size in byte will depend on your encoding, for exemple you can use:

System.Text.ASCIIEncoding.Unicode.GetByteCount(s);
System.Text.ASCIIEncoding.ASCII.GetByteCount(s);
...

Depending on the encoding you are using.

EDIT1: Is the amount of data significant ? Assuming you're sending it over the wire, you have many options to compress data before sending it.

You can use you own custom encoding to reduce the data sent as much as possible, like say indexing constant node names to "a", "b"... and sending the map after. You can also try to send it in chunk.

If file size is an issue because of timeout, I recommend looking into the webservice.timeout property On MSDN

You probably want to investigate System.IO.Compression.GZipStream . Your web service will need to know to decompress it using the same class.

Do some benchmarking before and after the compression. You may find that the zipping/unzipping at each end of the pipe adds more time than what you saved by chopping some bytes from the payload. You need to find the sweet spot where zipping saves you more time than it costs (1/2 a MB, especially over a LAN, is not a lot of traffic, unless you are really pumping those xml documents through).

I'd some bad experience with .NET compression libraries in past, so I recommend SharpZipLib . You'll need to change your web method implementation to accept a byte[] ;

You can also to access that service as WCF service, using a TCP binding, so it'll consume less bandwidth. You should consider this option before messing with your web method signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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