字符串WebService上先zip压缩再base64编码后传输

本文探讨了在WebService中如何先对字符串进行zip压缩,然后进行base64编码,以提高数据传输效率。同时,也介绍了接收到编码后的字符串如何通过base64解码和解压还原原始内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先对字符串进行zip压缩,再进行base64编码

public string ZipBase64(string xml)
        {
            //压缩
            string compressStr = "";
            byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(xml);
            try
            {
                MemoryStream ms = new MemoryStream();
                GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
                zip.Write(compressBeforeByte, 0, compressBeforeByte.Length);
                zip.Close();
                byte[] buffer = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(buffer, 0, buffer.Length);
                ms.Close();
                compressStr= Convert.ToBase64String(buffer);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            //base64编码
            byte[] encData_byte = new byte[compressStr.Length];
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值