使用WebService压缩传输的心得
发布时间:2020-12-16 23:33:49 所属栏目:安全 来源:网络整理
导读:? ? ?现在对WebService技术的使用已经非常多。既然是Web技术,就免不了要进行数据传输,比如传输文本、图片、Zip压缩包等等。在网络环境比较好、数据量小的情况下,传输的方式可以忽略不计,你可以选择任意的方式,不要考虑优化的问题,可以将图片、包变成二
?
?
?
?
以传输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:imagestest.gif"); respContext.Attachments.Add(dimeAttach); }
3、客户端引用
using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2;
改变改变Reference.cs Reference.cs 文件中代理类的基类为文件中代理类的基类为
Microsoft.Web.Services2.WebServicesClientProtoco
如何找到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?现在对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, 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", @"C:imagestest.gif"); respContext.Attachments.Add(dimeAttach); }
3、客户端引用
using Microsoft.Web.Services2.Dime;
using Microsoft.Web.Services2;
改变改变Reference.cs Reference.cs 文件中代理类的基类为文件中代理类的基类为
Microsoft.Web.Services2.WebServicesClientProtoco
如何找到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
?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |