欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > asp.net >内容正文

asp.net

asp.net之soap的发送、接收与处理类 [转载] -凯发k8官方网

发布时间:2024/10/8 asp.net 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 asp.net之soap的发送、接收与处理类 [转载] 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

首先本文不对soap有过多的解释,你只需知道它是--简单对象访问协议,一种轻量的、简单的、基于 xml 的协议。举个例子,webservice其实就基于soap的。

如果你需要了解soap的运行机制,请看这里:http://www.51script.com/?p=932

再简单的说一下,soap就是打包-->发送-->收包-->处理-->返回包等一系列流程,在asp.net中可以使用msxml2中的xmlhttpclass类来创建soap发送对象,先下载interop.msxml2.dll,然后复制到vs项目的bin目录,或者在vs里添加引用。创建如下代码:

view sourceprint?
001.#region 引用的命名空间
002.using system;
003.using system.io;
004.using system.data;
005.using system.web;
006.using system.text.regularexpressions;
007.using system.collections.generic;
008.using system.text;
009.using system.xml;
010.using msxml2;//xmlhttp所属命名空间(添加引用:com->microsoft xml 3.0)
011.#endregion
012. 
013.namespace simple.soap
014.{
015.    #region send:处理 xml 数据的发送。
016.    ///


017.    /// 处理 xml 数据的发送。
018.    ///

019.    public class send
020.    {
021.        ///
022.        /// xmlhttp 对象。
023.        ///

024.        private xmlhttp xmlhttp = new xmlhttpclass();
025.        ///
026.        /// send 失败后的提示信息。
027.        ///

028.        private string _error = "";
029.        ///
030.        /// 发送数据包的字符串表现形式。
031.        ///

032.        private string _data = "";
033.        ///
034.        /// send 返回数据包的字符串表现形式。
035.        ///

036.        private string _return = "";
037. 
038.        ///
039.        /// 获取或设置发送数据包的字符串表现形式(建议发送xml数据文档的字符串表现形式)。
040.        ///

041.        public string data
042.        {
043.            get
044.            {
045.                return _data;
046.            }
047.            set
048.            {
049.                _data = value;
050.            }
051.        }
052.        ///
053.        /// 获取 send 返回数据包的字符串表现形式。
054.        ///

055.        public string return
056.        {
057.            get
058.            {
059.                return _return;
060.            }
061.        }
062.        ///
063.        /// 获取 send 失败后的提示信息。
064.        ///

065.        public string error
066.        {
067.            get
068.            {
069.                return _error;
070.            }
071.        }
072. 
073.        ///
074.        /// 初始化 send 类的新实例。
075.        ///

076.        public send()
077.        {
078.        }
079.        ///
080.        /// 初始化 send 类的新实例。
081.        ///

082.        /// 要发送数据的字符串表现形式(建议发送 xml 数据文档的字符串表现形式)。
083.        public send(string data)
084.        {
085.            _data = data;
086.        }
087. 
088.        ///
089.        /// 发送数据。
090.        ///

091.        /// 要发送到的url路径字符串。
092.        /// true 表示发送成功,false 发送失败。
093.        public bool executesend(string url)
094.        {
095.            return executesend("post", url);
096.        }
097.        ///
098.        /// 发送数据。
099.        ///

100.        /// 要发送到的 url 路径字符串。
101.        /// 发送方式。
102.        /// true 表示发送成功,false 发送失败。
103.        public bool executesend(string url, string method)
104.        {
105.            bool b = false;
106.            xmlhttp.open(method, url, false, null, null);
107.            xmlhttp.send(_data);
108.            if (xmlhttp.readystate == 4)
109.            {
110.                if (xmlhttp.status == 200)
111.                {
112.                    _return = xmlhttp.responsetext;
113.                    b = true;
114.                }
115.                else
116.                {
117.                    _error = "请求页面有异常(xmlhttp.status=" xmlhttp.status ")。";
118.                }
119.            }
120.            else
121.            {
122.                _error = "请求页面有异常(xmlhttp.readystate=" xmlhttp.readystate ")。";
123.            }
124.            return b;
125.        }
126.    }
127.    #endregion
128. 
129.    #region incept:处理 xml 数据的接收。
130.    ///
131.    /// 处理 xml 数据的接收。
132.    ///

133.    public static class incept
134.    {
135.        ///
136.        /// 获取接收的数据包,并使用指定的编码对数据包进行解码。
137.        ///

138.        /// 数据包的字符串形式。
139.        public static string executeincept()
140.        {
141.            return executeincept(encoding.getencoding("utf-8"));
142.        }
143.        ///
144.        /// 获取接收的数据包,并使用指定的编码对数据包进行解码。
145.        ///

146.        /// 与首选编码相对应的代码页值。
147.        /// 数据包的字符串形式。
148.        public static string executeincept(int encode)
149.        {
150.            return executeincept(encoding.getencoding(encode));
151.        }
152.        ///
153.        /// 获取接收的数据包,并使用指定的编码对数据包进行解码。
154.        ///

155.        /// 字符编码的名称。
156.        /// 数据包的字符串形式。
157.        public static string executeincept(string encode)
158.        {
159.            return executeincept(encoding.getencoding(encode));
160.        }
161.        ///
162.        /// 获取接收的数据包,并使用指定的编码对数据包进行解码。
163.        ///

164.        /// 字符编码对象实例。
165.        /// 数据包的字符串形式。
166.        public static string executeincept(encoding encode)
167.        {
168.            streamreader sr = new streamreader(httpcontext.current.request.inputstream, encode);
169.            return sr.readtoend();
170.        }
171.    }
172.    #endregion
173. 
174.    #region return:处理 xml 数据的返回。
175.    ///
176.    /// 处理 xml 数据的返回。
177.    ///

178.    public static class return
179.    {
180.        ///
181.        /// 返回 xml 数据包。
182.        ///

183.        /// 要返回的 xml 的字符串表现形式。
184.        public static void executereturn(string body)
185.        {
186.            executereturn(body, "utf-8");
187.        }
188.        ///
189.        /// 返回 xml 数据包。
190.        ///

191.        /// 要返回的 xml 的字符串表现形式。
192.        /// 输出字符的编码格式。
193.        public static void executereturn(string body, string encode)
194.        {
195.            if (!new regex(@"^<\\\?xml.*\\\?>.*$", regexoptions.ignorecase).ismatch(regex.escape(body)))
196.            {
197.                body = "\n" body;
198.            }
199.            else
200.            {
201.                string start = body.substring(0, body.indexof("\"?>"));
202.                string left = body.substring(0, start.lastindexof("\"") 1);
203.                body = left encode body.substring(body.indexof("\"?>"));
204.            }
205.            httpcontext.current.response.clear();
206.            httpcontext.current.response.buffer = true;
207.            httpcontext.current.response.contenttype = "text/xml";
208.            httpcontext.current.response.charset = encode;
209.            httpcontext.current.response.expires = 0;
210.            httpcontext.current.response.write(body);
211.            httpcontext.current.response.end();
212.        }
213.    }
214.    #endregion
215.}
如果你已经读过上一篇:asp之soap的发送、接收与处理类 ,那么你现在已经知道如何使用这三个类了。很简单,创建send发送类,并发送xml数据包,在接收站点中,创建incept接收包,并读取和处理发送站点传递过来的数据包,然后再创建return返回类,并返回(打印)数据包,最后在发送站点中读取这个返回包并处理,ok了。。这就是soap流程。

 

本文来自csdn博客,转载请标明出处:http://blog.csdn.net/xz2001/archive/2009/05/31/4228582.aspx

转载于:https://www.cnblogs.com/ddlzq/archive/2009/11/24/1609821.html

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发k8官方网为你收集整理的asp.net之soap的发送、接收与处理类 [转载]的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图