阅读 485

ASP.NET之SOAP的发送、接收与处理类

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

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

#region 引用的命名空间
using System;
using System.IO;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MSXML2;//xmlHttp所属命名空间(添加引用:COM->Microsoft Xml 3.0)
#endregion

namespace Simple.SOAP
{
#region Send:处理 XML 数据的发送。
///


/// 处理 XML 数据的发送。
///

public class Send
{
///
/// XMLHTTP 对象。
///

private XMLHTTP xmlHttp = new XMLHTTPClass();
///
/// Send 失败后的提示信息。
///

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

private string _data = "";
///
/// Send 返回数据包的字符串表现形式。
///

private string _return = "";

///
/// 获取或设置发送数据包的字符串表现形式(建议发送XML数据文档的字符串表现形式)。
///

public string Data
{
get
{
return _data;
}
set
{
_data = value;
}
}
///
/// 获取 Send 返回数据包的字符串表现形式。
///

public string Return
{
get
{
return _return;
}
}
///
/// 获取 Send 失败后的提示信息。
///

public string Error
{
get
{
return _error;
}
}

///
/// 初始化 Send 类的新实例。
///

public Send()
{
}
///
/// 初始化 Send 类的新实例。
///

/// 要发送数据的字符串表现形式(建议发送 XML 数据文档的字符串表现形式)。
public Send(string data)
{
_data = data;
}

///
/// 发送数据。
///

/// 要发送到的Url路径字符串。
/// true 表示发送成功,false 发送失败。
public bool ExecuteSend(string url)
{
return ExecuteSend("POST", url);
}
///
/// 发送数据。
///

/// 要发送到的 url 路径字符串。
/// 发送方式。
/// true 表示发送成功,false 发送失败。
public bool ExecuteSend(string url, string method)
{
bool b = false;
xmlHttp.open(method, url, false, null, null);
xmlHttp.send(_data);
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
_return = xmlHttp.responseText;
b = true;
}
else
{
_error = "请求页面有异常(XMLHTTP.status=" + xmlHttp.status + ")。";
}
}
else
{
_error = "请求页面有异常(XMLHTTP.readyState=" + xmlHttp.readyState + ")。";
}
return b;
}
}
#endregion

#region Incept:处理 XML 数据的接收。
///
/// 处理 XML 数据的接收。
///

public static class Incept
{
///
/// 获取接收的数据包,并使用指定的编码对数据包进行解码。
///

/// 数据包的字符串形式。
public static string ExecuteIncept()
{
return ExecuteIncept(Encoding.GetEncoding("utf-8"));
}
///
/// 获取接收的数据包,并使用指定的编码对数据包进行解码。
///

/// 与首选编码相对应的代码页值。
/// 数据包的字符串形式。
public static string ExecuteIncept(int encode)
{
return ExecuteIncept(Encoding.GetEncoding(encode));
}
///
/// 获取接收的数据包,并使用指定的编码对数据包进行解码。
///

/// 字符编码的名称。
/// 数据包的字符串形式。
public static string ExecuteIncept(string encode)
{
return ExecuteIncept(Encoding.GetEncoding(encode));
}
///
/// 获取接收的数据包,并使用指定的编码对数据包进行解码。
///

/// 字符编码对象实例。
/// 数据包的字符串形式。
public static string ExecuteIncept(Encoding encode)
{
StreamReader sr = new StreamReader(HttpContext.Current.Request.InputStream, encode);
return sr.ReadToEnd();
}
}
#endregion

#region Return:处理 XML 数据的返回。
///
/// 处理 XML 数据的返回。
///

public static class Return
{
///
/// 返回 XML 数据包。
///

/// 要返回的 XML 的字符串表现形式。
public static void ExecuteReturn(string body)
{
ExecuteReturn(body, "utf-8");
}
///
/// 返回 XML 数据包。
///

/// 要返回的 XML 的字符串表现形式。
/// 输出字符的编码格式。
public static void ExecuteReturn(string body, string encode)
{
if (!new Regex(@"^.*$", RegexOptions.IgnoreCase).IsMatch(Regex.Escape(body)))
{
body = "/n" + body;
}
else
{
string start = body.Substring(0, body.IndexOf("/"?>"));
string left = body.Substring(0, start.LastIndexOf("/"") + 1);
body = left + encode + body.Substring(body.IndexOf("/"?>"));
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.Charset = encode;
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.Write(body);
HttpContext.Current.Response.End();
}
}
#endregion
}

 

原文:https://www.cnblogs.com/LiangIT12138/p/14898085.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐