WebService压缩传输

来源:互联网 发布:软件测试有哪些 编辑:程序博客网 时间:2024/05/18 15:30
参考了 博客园 黎波的博客 在此表示感谢!
 
  现在对WebService技术的使用已经非常多。既然是Web技术,就免不了要进行数据传输,比如传输文本、图片、Zip压缩包等等。在网络环境比较好、数据量小的情况下,传输的方式可以忽略不计,你可以选择任意的方式,不要考虑优化的问题,可以将图片、包变成二进制流进行传输,可以从数据库读出数据来,以DataSet形式进行传输。
 
  但是如果网络条件不好,或者数据量很大的情况下,应该如何做呢?
 
以传输DataSet为例,解决方案如下:
1、从数据库读取DataSet
2、使用DataSetSurrogate类序列化DataSet,转换成二维数组,一般能变成原来大小的1/3
3、使用SharpZipLib压缩
此时如果在网络上传输字节数组的话,又会讲过Base64编码,造成大小的反弹
4、使用Web Services Enhancements的WS-Attachment进行传输。将包放到SOAP附件里,而不是信封里,不用经过XML序列化。封成DIME消息(Direct Internet Message Encapsulation)
 
这样,原来的大数据就可以被封成很小的数据了。传输也变得简单、迅速。
=======================================================
使用DIME进行传输的必要条件
1、安装Microsoft WSE 2.0 SP3.msi,可以在微软的网站上下
 
2、服务器端实现
 
1) 添加Microsoft.Web.Services2.dll Microsoft.Web.Services2.dll 引用
 

2) 修改修改Web.config Web.config 配置文件配置文件

<configuration><system.web><webServices><soapExtensionTypes><add type="Microsoft.Web.Services2.WebServicesExtension,Microsoft.Web.Services2, Version=2.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"priority="1" group="0" /></soapExtensionTypes></webServices></system.web></configuration> 

3)WebMethod实现

using Microsoft.Web.Services2.Dime;using Microsoft.Web.Services2; [WebMethod]public void CreateDimedImage(){SoapContext respContext = ResponseSoapContext.Current;DimeAttachment dimeAttach = new DimeAttachment("image/gif", TypeFormat.MediaType,@"C:\images\test.gif");respContext.Attachments.Add(dimeAttach);}

3、客户端引用

using Microsoft.Web.Services2.Dime;using Microsoft.Web.Services2;

  
改变改变Reference.cs Reference.cs 文件中代理类的基类为文件中代理类的基类为
Microsoft.Web.Services2.WebServicesClientProtocol
 
如何找到Reference.cs?
在VS的资源管理器上选择“显示所有文件”,在Web Service下的Reference下有
 
实现例子
private void btnGetImage_Click(object sender, EventArgs e){MyDimeServiceWse svc = new MyDimeServiceWse();svc.CreateDimedImage();if (svc.ResponseSoapContext.Attachments.Count == 1){MessageBox.Show("Got it!\n");pbDime.Image = new Bitmap(svc.ResponseSoapContext.Attachments[0].Stream);}}

转自:http://blog.sina.com.cn/s/blog_4b22165e01000774.html