在编写web service时,要考虑传输速度,就要注意对大数据量文件或图片进行压缩传输,然后在客户端进行解压缩,可以减少传输过程中的时间!
以下是主要代码:
以下是主要代码:
1//服务端:
2 [WebMethod]
3 public byte[] Search(string ID)
4 {
5 // 压缩
6 MemoryStream oStream = new MemoryStream();
7 DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);
8 zipStream.Write(bytes, 0, bytes.Length);
9 zipStream.Flush();
10 zipStream.Close();
11 return oStream.ToArray();
12 }
13
14//客户端:
15public DataSet DecompressionDataSet(byte[] bytes)
16 {
17 // 初始化流,设置读取位置
18 MemoryStream mStream = new MemoryStream(bytes);
19 mStream.Seek(0, SeekOrigin.Begin);
20 // 解压缩
21 DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
22 // 反序列化得到数据集
23 DataSet dsResult = new DataSet();
24 dsResult.RemotingFormat = SerializationFormat.Binary;
25 BinaryFormatter bFormatter = new BinaryFormatter();
26 dsResult = (DataSet)bFormatter.Deserialize(unZipStream);
27 return dsResult;
28 }
2 [WebMethod]
3 public byte[] Search(string ID)
4 {
5 // 压缩
6 MemoryStream oStream = new MemoryStream();
7 DeflateStream zipStream = new DeflateStream(oStream, CompressionMode.Compress);
8 zipStream.Write(bytes, 0, bytes.Length);
9 zipStream.Flush();
10 zipStream.Close();
11 return oStream.ToArray();
12 }
13
14//客户端:
15public DataSet DecompressionDataSet(byte[] bytes)
16 {
17 // 初始化流,设置读取位置
18 MemoryStream mStream = new MemoryStream(bytes);
19 mStream.Seek(0, SeekOrigin.Begin);
20 // 解压缩
21 DeflateStream unZipStream = new DeflateStream(mStream, CompressionMode.Decompress, true);
22 // 反序列化得到数据集
23 DataSet dsResult = new DataSet();
24 dsResult.RemotingFormat = SerializationFormat.Binary;
25 BinaryFormatter bFormatter = new BinaryFormatter();
26 dsResult = (DataSet)bFormatter.Deserialize(unZipStream);
27 return dsResult;
28 }