使用WebService压缩传输的心得

本文探讨了在不同网络条件下,使用WebService技术高效传输大数据的策略。通过序列化、压缩和使用DIME消息封装,实现了大数据的高效传输,显著提高了传输效率。

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

 
   现在对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 现在对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
   现在对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
   现在对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);
}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值