webservice端取得输入参数和返回值
来源:互联网 发布:dell风扇控制软件 编辑:程序博客网 时间:2024/06/06 00:13
来源:[url=http://it.chinawin.net/softwaredev/article-2377.html]http://it.chinawin.net/softwaredev/article-2377.html[/url] 日前遇到一个问题,需要在webservice端取得输入参数和返回值,在网上搜索后没发现类似的解决方法,没办法了只好自己解决。输入参数在webservice初期化时可以取得,但返回值如何取得呢?我们知道webservice返回的是xml格式的数据流,那么我们在他返回前获得这个对象不就可以读出返回值了吗?这里要特别注意的是Response.OutpurStream是不可读,所以只能自定义过滤器来获得。using System;using System.Collections.Generic;using System.Text;using System.ComponentModel;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Configuration;using System.Xml;using System.IO;namespace Framework{ /// /// webservice基类 /// public class BaseWebService : System.Web.Services.WebService { Logger _log; public BaseWebService() { //webservice结束时 this.Disposed += new EventHandler(Dispose); //输入参数 System.Collections.Specialized.NameValueCollection methodParameters = HttpContext.Current.Request.Form; string methodName = HttpContext.Current.Request.PathInfo.TrimStart(new char[] { '/' }); Dictionary paras = new Dictionary(); foreach (string name in methodParameters) { paras.Add(name, methodParameters[name]); } string path = ConfigurationManager.AppSettings["LogPath"].ToString(); path = Server.MapPath(path); //将输入参数写到日志 this._log = new Logger(path, this.ToString(), methodName); this._log.WriteXmlLog(paras); } /**/ /// /// 对此HTTP请求处理的过程全部结束 /// /// /// void Dispose(object sender, EventArgs e) { HttpApplication application = this.Context.ApplicationInstance; //装配过滤器 application.Response.Filter = new RawFilter(application.Response.Filter); //绑定过滤器事件 RawFilter filter = (RawFilter)application.Response.Filter; filter.OnRawDataRecordedEvent += new EventHandler(filter_OnRawDataRecordedEvent); } /**/ /// /// 当原始数据采集到以后,写日志 /// /// /// void filter_OnRawDataRecordedEvent(object sender, RawDataEventArgs e) { string allcode = e.SourceCode; try { Dictionary dicRet = new Dictionary(); XmlDocument document = new XmlDocument(); try { document.LoadXml(allcode); string retValue = document.DocumentElement.InnerXml; if (!string.IsNullOrEmpty(retValue)) { dicRet.Add("Return", retValue); } else { dicRet.Add("Return", ""); } this._log.WriteXmlLog(dicRet); } catch (XmlException) { dicRet.Add("Return", ""); this._log.WriteXmlLog(dicRet); } } catch (Exception) { throw; } } } /**/ /// /// 定义原始数据EventArgs,便于在截获完整数据后,由事件传递数据 /// public class RawDataEventArgs : EventArgs { private string sourceCode; public RawDataEventArgs(string SourceCode) { sourceCode = SourceCode; } public string SourceCode { get { return sourceCode; } set { sourceCode = value; } } } //自定义过滤器 public class RawFilter : Stream { Stream responseStream; long position; StringBuilder responseHtml; /**/ /// /// 当原始数据采集成功后激发。 /// public event EventHandler OnRawDataRecordedEvent; public RawFilter(Stream inputStream) { responseStream = inputStream; responseHtml = new StringBuilder(); } //实现Stream 虚方法 #region Filter Overrides public override bool CanRead { get { return true; } } public override bool CanSeek { get { return true; } } public override bool CanWrite { get { return true; } } public override void Close() { responseStream.Close(); } public override void Flush() { responseStream.Flush(); } public override long Length { get { return 0; } } public override long Position { get { return position; } set { position = value; } } public override int Read(byte[] buffer, int offset, int count) { return responseStream.Read(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) { return responseStream.Seek(offset, origin); } public override void SetLength(long length) { responseStream.SetLength(length); } #endregion //关键的点,在HttpResponse 输入内容的时候,一定会调用此方法输入数据,所以要在此方法内截获数据 public override void Write(byte[] buffer, int offset, int count) { string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count); ////采用正则,检查输入的是否有页面结束符 //Regex eof = new Regex("", RegexOptions.IgnoreCase); //if (!eof.IsMatch(strBuffer)) //{ // //页面没有输出完毕,继续追加内容 // responseHtml.Append(strBuffer); //} //else { //页面输出已经完毕,截获内容 responseHtml.Append(strBuffer); string finalHtml = responseHtml.ToString(); //激发数据已经获取事件 OnRawDataRecordedEvent(this, new RawDataEventArgs(finalHtml)); //继续传递要发出的内容写入流 byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml); responseStream.Write(data, 0, data.Length); } } }} (中赢网chinawin.net)
- webservice端取得输入参数和返回值
- ExecuteReader如何取得输出参数和返回值
- java webservice server 和client 参数和返回值
- C++(4):函数的输入参数和返回值
- WebService传多个参数和返回多个参数的方法
- android调用webservice方法,参数和返回值都用字符串
- .net 异步调用webservice 并取得Webservice返回结果
- SQLDataSource 取得存储过程返回参数
- 利用SQLDataSource取得存储过程返回参数
- SQLDataSource 取得存储过程返回参数
- ASP.NET中调用存储过程带(输入/输出)参数和返回值
- ASP.NET中调用存储过程带(输入/输出)参数和返回值
- c++是如何处理 对象 作为返回值 和 输入参数的 -- 拷贝构造
- 关于多线程输入参数和返回值的几种情况
- java和ibatis调用存储过程并取得返回值
- java和ibatis调用存储过程并取得返回值
- java和ibatis调用存储过程并取得返回值
- java和ibatis调用存储过程并取得返回值
- 揪出你找不到的系统头文件
- 用铂为标准材料研究热点偶
- SQL函数说明大全
- 动态ip 投票程序
- non-local static 变量初始化顺序不确定,带来的问题
- webservice端取得输入参数和返回值
- 关于MAP文件的使用(转贴)
- USB OTG技术的发展现况
- c 语言二维数组传址 指针方式
- 安装flashplugin-installer报错
- Windows下的Memcache安装
- DEDECMS最新5.7版在Windows下的Memcache安装
- Linux热插拔处理机制
- controll 向view传值