惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - Bryant

SQL 2005 Reporting Service "提供程序加载失败" Provider Load fail的解决 Help:立体图绘制以及根据X,Y,Z三坐标值,在图上描点 winform 统计图控件 解决 VS2005 SP1 时“无法使用此产品的安装源,请确认安装源存在,并且您可以访问它”的错误 控件开发的资料 疑问:公路移动应用开发;数据挖掘开发,哪个更好? 2008年的第一场雪,丰收之雪 Visual Studio 2005 SP1安装 关键点 终极解决:Cab安装包的时候,制作中文快捷方式,用中文编译会报错,用英文做为快捷方式名 编译器错误信息: CS0016: 未能写入输出文件 中秋节快乐 创建.net web项目出错HTTP/1.1 500 Internal Server Error 获得字符串长度(中文字符占2) - Bryant - 博客园 sql Server 索引优化 (转) Infragistics的部署问题 Happy New Year FreeTextBox控件 保存空格 显示确是? 控件开发中常用的元数据 结果怎么会是这样?
WebService 数据压缩
Bryant · 2007-04-10 · via 博客园 - Bryant

很长时间没有更新博客了
一直在忙这做项目,累啊,苦啊
现在刚好忙完了,赶紧看看兄弟姐妹们的博客,其中看了刚刚兄有关Web服务初探:用Demo学Web服务系列,使自己才更清楚的了解了WebService,Thx!
然而项目中有时数据大,所以在网上搜了个压缩数据的,特此贴出来,方便自己日后用到,也希望能给大家一些帮助,呵呵
CompressionExtension.cs:

using System;
using System.IO;
using System.Text ;
using System.Web.Services;
using System.Web.Services.Protocols ;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using System.Xml ;

namespace BLL
{
    
/// <summary>
    
/// Summary description for ConpressionExtension.
    
/// </summary>

    public class CompressionExtension : System.Web.Services.Protocols.SoapExtension
    
{
        Stream oldStream;
        Stream newStream;
    

        
public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) 
        
{
            
return attribute;
        }


        
// Get the Type
        public override object GetInitializer(Type t) 
        
{
            
return typeof(CompressionExtension);
        }

    
        
// Get the CompressionExtensionAttribute
        public override void Initialize(object initializer) 
        
{
            CompressionExtensionAttribute attribute 
= (CompressionExtensionAttribute) initializer;

            
return;
        }


        
// Process the SOAP Message
        public override void ProcessMessage(SoapMessage message) 
        
{
            
// Check for the various SOAP Message Stages 
            switch (message.Stage) 
            
{

                
case SoapMessageStage.BeforeSerialize:
                    
break;

                
case SoapMessageStage.AfterSerialize:
                    
// ZIP the contents of the SOAP Body after it has
                    
// been serialized
                    Zip();
                    
break;

                
case SoapMessageStage.BeforeDeserialize:
                    
// Unzip the contents of the SOAP Body before it is
                    
// deserialized
                    Unzip();
                    
break;

                
case SoapMessageStage.AfterDeserialize:
                    
break;

                
default:
                    
throw new Exception("invalid stage");
            }

        }


        
// Gives us the ability to get hold of the RAW SOAP message
        public override Stream ChainStream( Stream stream ) 
        
{
            oldStream 
= stream;
            newStream 
= new MemoryStream();
            
return newStream;
        }


        
// Utility method to copy streams
        void Copy(Stream from, Stream to) 
        
{
            TextReader reader 
= new StreamReader(from);
            TextWriter writer 
= new StreamWriter(to);
            writer.WriteLine(reader.ReadToEnd());
            writer.Flush();
        }


    
        
// Zip the SOAP Body
        private void Zip() 
        
{
            newStream.Position 
= 0;
            
// Zip the SOAP Body
            newStream = ZipSoap(newStream);
            
// Copy the streams
            Copy(newStream, oldStream);
        }


        
// The actual ZIP method
        private byte[] Zip(string stringToZip) 
        
{
            
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToZip);
            MemoryStream ms 
= new MemoryStream();

            
// Check the #ziplib docs for more information
            ZipOutputStream zipOut = new ZipOutputStream( ms ) ;
            ZipEntry ZipEntry 
= new ZipEntry("ZippedFile");
            zipOut.PutNextEntry(ZipEntry);
            zipOut.SetLevel(
9);
            zipOut.Write(inputByteArray, 
0 , inputByteArray.Length ) ;     
            zipOut.Finish();
            zipOut.Close();

            
// Return the zipped contents
            return ms.ToArray();
        }


        
// Select and Zip the appropriate parts of the SOAP message
        public MemoryStream ZipSoap(Stream streamToZip) 
        
{
            streamToZip.Position 
= 0;
            
// Load a XML Reader
            XmlTextReader reader = new XmlTextReader(streamToZip);
            XmlDocument dom 
= new XmlDocument();
            dom.Load(reader);
            
// Load a NamespaceManager to enable XPath selection
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(dom.NameTable);
            nsmgr.AddNamespace(
"soap""http://schemas.xmlsoap.org/soap/envelope/");
            XmlNode node 
= dom.SelectSingleNode("//soap:Body", nsmgr);
            
// Select the contents within the method defined in the SOAP body
            node = node.FirstChild.FirstChild;
            
// Check if there are any nodes selected
            while( node != null )
            
{
                
if( node.InnerXml.Length > 0 )
                
{
                    
// Zip the data
                    byte[] outData = Zip(node.InnerXml);
                    
// Convert it to Base64 for transfer over the internet
                    node.InnerXml = Convert.ToBase64String(outData) ;
                }

                
// Move to the next parameter
                node = node.NextSibling ;
            }

            MemoryStream ms 
= new MemoryStream();
            
// Save the updated data
            dom.Save(ms);
            ms.Position 
= 0;
      
            
return ms;
        }


        
// Unzip the SOAP Body
        private void Unzip() 
        
{
            MemoryStream unzipedStream 
= new MemoryStream();
        
            TextReader reader 
= new StreamReader(oldStream);
            TextWriter writer 
= new StreamWriter(unzipedStream);
            writer.WriteLine(reader.ReadToEnd());
            writer.Flush();
            
// Unzip the SOAP Body
            unzipedStream = UnzipSoap(unzipedStream);
            
// Copy the streams
            Copy(unzipedStream, newStream);
     
            newStream.Position 
= 0;
        }


        
// Actual Unzip logic
        private byte[] Unzip(string stringToUnzip) 
        
{
            
// Decode the Base64 encoding
            byte[] inputByteArray = Convert.FromBase64String( stringToUnzip ) ;
            MemoryStream ms 
= new MemoryStream(inputByteArray) ;
            MemoryStream ret 
= new MemoryStream();

            
// Refer to #ziplib documentation for more info on this
            ZipInputStream zipIn = new ZipInputStream(ms);
            ZipEntry theEntry 
= zipIn.GetNextEntry();
            Byte[] buffer 
= new Byte[2048] ;
            
int size = 2048;
            
while (true
            
{
                size 
= zipIn.Read(buffer, 0, buffer.Length);
                
if (size > 0
                
{
                    ret.Write(buffer, 
0, size);
                }
 
                
else 
                
{
                    
break;
                }

            }

            
return ret.ToArray();
        }


        
// Unzip the SOAP Body
        public MemoryStream UnzipSoap(Stream streamToUnzip) 
        
{
            streamToUnzip.Position 
= 0;
            
// Load a XmlReader
            XmlTextReader reader = new XmlTextReader(streamToUnzip);
            XmlDocument dom 
= new XmlDocument();
            dom.Load(reader);

            XmlNamespaceManager nsmgr 
= new XmlNamespaceManager(dom.NameTable);
            nsmgr.AddNamespace(
"soap""http://schemas.xmlsoap.org/soap/envelope/");
            
// Select the SOAP Body node 
            XmlNode node = dom.SelectSingleNode("//soap:Body", nsmgr);
            node 
= node.FirstChild.FirstChild;

            
// Check if node exists
            while( node != null )
            
{
                
if( node.InnerXml.Length >0 )
                
{
                    
// Send the node's contents to be unziped
                    byte[] outData = Unzip(node.InnerXml);
                    
string sTmp = Encoding.UTF8.GetString(outData);
                    node.InnerXml 
= sTmp;
                }

                
// Move to the next parameter 
                node = node.NextSibling ;
            }


            MemoryStream ms 
= new MemoryStream();
      
            dom.Save(ms);
            ms.Position 
= 0;

            
return ms;
        }


    }

}

CompressionExtensionAttribute.cs:

using System;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace BLL
{

    
/// <summary>
    
/// Summary description for CompressionExtensionAttribute.
    
/// </summary>

    
    
// Make the Attribute only Applicable to Methods
    [AttributeUsage(AttributeTargets.Method)]
    
public class CompressionExtensionAttribute : System.Web.Services.Protocols.SoapExtensionAttribute
    
{
    
        
private int priority;

        
// Override the base class properties
        public override Type ExtensionType 
        
{
            
get return typeof(CompressionExtension); }
        }


        
public override int Priority 
        
{
            
get 
            

                
return priority; 
            }

            
set 
            

                priority 
= value; 
            }

        }


    }

}

在server | Client 都需要
调用很简单,只要在代理类中web方法加个:

[CompressionExtension]

原文地址:http://www.123aspx.com/redir.aspx?res=29459