C#开发XML WebService接口(SOAP)
本文由80368704贡献
C#开发 XML WebService 接口 开发 接口(SOAP)
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Web; using System.Web.Services; using BX.Interface;
namespace WebService { /// <summary> /// Service1 的摘要说明。 /// </summary> public class doService : System.Web.Services.WebService {
/// <summary> /// 网上报销系统 WebService 服务通用调用方法 /// </summary> /// <param name="ifCode">接口标识</param> /// <param name="ifPass">校验码</param> /// <param name="ifEvent">操作方式(可空)</param> /// <param name="ifInfo">传入的 XML 信息</param> /// <returns></returns> [WebMethod] public string IfService(string ifCode,string ifPass,string ifEvent,string ifInfo) { ifCode = ifCode.Trim(); ifPass = ifPass.Trim(); ifEvent = ifEvent.Trim(); ifInfo = ifInfo.Trim();
string IReceiveContract_Id = System.Configuration.ConfigurationSettings.A ppSettings["IReceiveContract_Id"].Trim(); string IReceiveContract_PSW = System.Configuration.Config
urationSettings.
AppSettings["IReceiveContract_PSW"].Trim(); string IReceiveNoContract_Id = System.Configuration.ConfigurationSettings. AppSettings["IReceiveNoContract_Id"].Trim(); string IReceiveNoContract_PSW = System.Configuration.ConfigurationSettin gs.AppSettings["IReceiveNoContract_PSW"].Trim();
if(ifCode.Equals(IReceiveContract_Id)) { if(!ifPass.Equals(IReceiveContract_PSW)) ateResultXML(2,"",""); return IApplyContract.ReceiveApply(ifCode,ifInfo); } else if(ifCode.Equals(IReceiveNoContract_Id)) { if(!ifPass.Equals(IReceiveNoContract_PSW)) ateResultXML(2,"",""); return IApplyNoContract.ReceiveApply(ifCode,ifInfo); } else { ateResultXML(1,"",""); } } //-- end IfSerive } }
// **************************************************** // // // // // 功 说 能:SOAP XML WebService 接口通用处理类 明:
创 建 者:dingzh@jstrd 创建时间:2007-07-04 修改信息:
// **************************************************** using System; using System.Xml; using System.Web.Services.Description;
using System.CodeDom; using System.CodeDom.Compiler; using System.Data;
namespace BX.Interface { /// <summary> /// SOAP XML WebService 接口通用处理类 /// </summary> public class ICommon { public ICommon() { // // TODO: 在此处添加构造函数逻辑 // }
/// <summary> /// 动态引用一个 XML Web Service /// IfSerive(string ifCode,string ifPass,string ifEvent,string ifInfo) /// </summary> /// <param name="v_WSDL">WSDL 路径</param> /// <param name="SvClsName">接口服务真实类名</param> /// <param name="v_WebMethod">接口函数方法名</param> /// <param name="v_ifCode">接口标识</param> /// <param name="v_args">接口函数参数列表数组</param> /// <returns>string</returns> public static string getSoapService(string v_WSDL, string SvClsNa
me, string v _WebMethod, string v_ifCode,ref object[] v_args)//不需安装 SOAP ToolKi
t 3.0 ! { string v_NameSpace = "DSv_"+v_ifCode; string v_ClassName = v_NameSpace + "." + SvClsName;
//1)用 XML 阅读器从一个文件路径或者是网络路径中读入 WSDL 文件: XmlTextReader reader = new XmlTextReader(v_WSDL);//加入 using System. Xml; //2〉把读到的内容用 ServiceDescription 来描述:
ServiceDescription sd = ServiceDescription.Read(reader); em.Web.Services.Description; //3)用服务导入器把得到的描述导入服务描述中:
//加入 using Syst
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd,null,null); //4)指定要动态编译的代码的命名空间: CodeNamespace cn = new CodeNamespace(v_NameSpace); rvices" //5)指定要动态编译的代码单元: CodeCompileUnit ccu = new CodeCompileUnit(); //6)把命名空间添加到动态编译的代码单元的命名空间组中去: ccu.Namespaces.Add(cn); //7)指示服务导入器把该动态编译单元内该命名空间的代码导进去,作为新的动态服务 描述: sdi.Import(cn,ccu); //8)新建一个 C#编译器来编译: ICodeCompiler icc = new Microsoft.CSharp.CSharpCodeProvider().CreateCo mpiler(); //9)新建一个编译参数对象(在这里我们设置它为空的编译参数): CompilerParameters cp=new CompilerParameters(); //10)指示 C#编译器,按照 CP 编译参数对象提供的参数来进行 CCU 单元的编译。并 把编译结果传给 编译结果对象 cr: CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu); //11)从动态编译结果中获取 某个 字符串(通常是该类型名字)所表示的真实类: Type t = cr.CompiledAssembly.GetType(v_ClassName); ervices 必须和 CodeNamespace cn = new 同名 //12)创建该类的实例: Object obj = Activator.CreateInstance(t); //13)根据方法名称字符串 反射到
该方法的实体: System.Reflection.MethodInfo mi = obj.GetType().GetMethod(v_WebMetho d); //14)用该方法实体的.Invoke()来调用该方法: string ret = System.Convert.ToString(mi.Invoke(obj, v_args)); //,new obje ct[]{"传给方法的参数 1","传给方法的参数 2"})); return ret; //其中的 DynamicS //"DynamicSe
CodeNamespace("DynamicServices");定义的
//
添加 Web 引用,在 WSDL 一栏中把刚才得到的 WSDL 地址复制过来,We
b 引用的名称输入 JavaService // e(); // o"); } //-- End getSoapService/ string aa = bean.IfService("ifUser","ifPass","ifCode","ifEvent","inf JavaService.ServiceService bean = new JavaService.ServiceServic
/// <summary> /// 创建返回结果 XML /// </summary> /// <param name="v_ifResult">接口结果代码</param> /// <param name="v_ifStack">错误信息堆栈</param> /// <param name="v_ifResultInfo">接口结果信息</param> /// <returns>string</returns> public static string createResultXML(int v_ifResult,string v_ifErrStack,string v_i f
ResultInfo) { string v_ifError = ""; switch(v_ifResult) { case 1: v_ifError = "无此接口标识 ifCode"
; break; case 2: v_ifError = "调用系统校验未通过"; break; case 3: v_ifError = "传入的不是一个正确的 xml 格式"; break; case 4: v_ifError = "info 参数中的字段不符合规约中的要求"; break; case 5: v_ifError = "该条数据已重复发送"; break; case 9:
v_ifError = "本系统处理时发生异常"; break; default: break; }
XmlDocument xmldoc; //XmlNode xmlnode; XmlElement xmlelem;
xmldoc = new XmlDocument(); //加入 XML 的声明段落,<?xml version="1.0" encoding="gb2312"?> XmlDeclaration xmldecl; xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null); xmldoc.AppendChild (xmldecl);
//加入一个根元素 xmlelem = xmldoc.CreateElement ("","IfResponse",""); xmldoc.AppendChild (xmlelem) ; //加入另外一个元素 //for(int i=1;i<3;i++) //{
XmlNode root = xmldoc.SelectSingleNode("IfResponse");//查<IfRespons e>
XmlElement eIfResult = xmldoc.CreateElement("IfResult");//创建一个<IfRes ult>节点 eIfResult.SetAtt
ribute("Note","接口结果代码"); //设置该节点 Note 属性 eIfResult.InnerText = v_ifResult.ToString(); //设置节点文本值 root.AppendChild(eIfResult); //添加到<IfResponse>节点中
XmlElement eIfError = xmldoc.CreateElement("IfError"); eIfError.SetAttribute("Note","错误信息"); eIfError.InnerText = v_ifError; root.AppendChild(eIfError);
XmlElement eIfErrorStack = xmldoc.CreateElement("IfErrorStack");
eIfErrorStack.SetAttribute("Note","错误信息堆栈"); eIfErrorStack.InnerText = v_ifErrStack; root.AppendChild(eIfErrorStack);
XmlElement eIfResultInfo = xmldoc.CreateElement("IfResultInfo "); eIfResultInfo.SetAttribute("Note","接口结果信息"); eIfResultInfo.InnerText = v_ifResultInfo; root.AppendChild(eIfResultInfo); //} //保存创建好的 XML 文档 //xmldoc.Save ( Server.MapPath("l") ) ;
return xmldoc.InnerXml; //xmldoc.OuterXml;
// // // // // // // // // //
<?xml version="1.0" encoding="GB2312"?> <IfResponse> <IfResult>接口结果代码</IfResult> <IfError>错误信息</IfError> <IfErrorStack>错误信息堆栈</IfErrorStack> <IfResultInfo>接口结果信息</IfResultInfo> </IfResponse>
<IfResult/>依照下述约定: 成功:0 —仅表示接口调用成功,并不代表业务调用成功,业务返回结果信息存放于<IfR
esultInfo/>节点中 // // // // // // // 失败:非零整数值 1:无此接口标识 ifCode 2:调用系统校验未通过 3:传入的不是一个正确的 xml 格式 4:info 参数中的字段不符合规约中的要求 5:该条数据已重复发送 9:本系统处理时发生异常 } //-- End createResultXML/
/// <summary> /// 将 XML 格式字符串转化成 DataSet /// </summary> /// <param name="xml">xml 字符串</param> /// <returns>DataSet</returns> public static DataSet parseResultXML2DataSet(string xml) { DataSet ds = new DataSet();
//方法一 using(Syst
em.IO.StringReader sr = new System.IO.StringReader(xml)) { ds.ReadXml(sr); }
// //
//方法二 using(System.Xml.XmlReader xr = System.Xm
l.XmlReader.Create(new Sy
stem.IO.StringReader(xml))) // // // } { ds.ReadXml(xr);
// //
//方法三 using(System.IO.Stream st = new System.IO.MemoryStream(System.Tex
t.Encoding.UTF8.GetBytes(xml))) // // // } { ds.ReadXml(st);
return ds; }
/// <summary> /// 将 XML 格式字符串转化成 IStructResult /// </summary> /// <param name="xml">xml 字符串</param> /// <returns>IStructResult</returns> public static IStructResult parseResultXML2Struct(string xml)
{ IStructResult structResult = new IStructResult(); structResult.Clear();
XmlDocument xmldoc = new XmlDocument(); try { xmldoc.LoadXml(xml); } catch(XmlException ex) {
structResult.IfResult = -1; structResult.IfError = "未能处理的异常返回结果"; structResult.IfErrorStack = ex.Message; return structResult; //throw ex; }
try { string strResult = xmldoc.GetElementsByTagName("IfResult").Item(0).In nerText.Trim(); structResult.IfResult trResult); structResult.IfError m(0).InnerText.Trim(); structResult.IfErrorStack ack").Item(0).InnerText.Trim(); structResult.IfResultInfo fo").Item(0).InnerText.Trim(); } catch(Exception e) { structResult.IfResult = -1; structResult.IfError = "未能处理的异常返回结果"; structResult.IfErrorStack = e.Message; return structResult; //throw e; = xmldoc.GetElementsByTagName("IfResultIn = xmldoc.GetElementsByTagName("IfErrorSt = xmldoc.GetElementsByTagName("IfError").Ite = strResult.Equals(string.Empty)?-1:int.Parse(s
} return structResult; } }
/// <summary> /// 接口返回结果结构体 /// </summary> public struct IStructResult { public int public string public string public string IfResult; IfError; IfErrorStack; IfResultInfo;
public void Clear() { IfResult IfError IfErrorStack IfResultInfo } } } = -1; = ""; = ""; = "";
using System; using System.Xml; using System.IO;
namespace BX.Interface { /// <summary> /// IApplyContract 的摘要说明。 /// </summary> public class IApplyContract
{ public IApplyContract() { // // TODO: 在此处添加构造函数逻辑 // }
/// <summary> /// 接收合同付款申请信息 /// </summary> /// <param name="xml"></param> /// <returns></returns> public static string ReceiveApply(string interfaceid, string xml) { XmlDocument xmldoc = new XmlDocument(); try { xmldoc.LoadXml(xml); } catch(XmlException ex) { //throw ex; ateResultXML(3,ex.Message,""); }
try { action = xmldoc.GetElementsByTagName("action").Item(0).InnerText.Tri m(); . . //XML 节点解析// . .
} catch(Exception e) { //throw e; ateResultXML(4,e.Message,""); }
string strSQL = "INSERT INTO dbo.bx_apply_interface () Values()"; try { //检测是否重复提交 string sqlJudge = "SELECT MAX(receive_time) AS lstTime FROM dbo.bx_ apply_interface"; sqlJudge += " WHERE prjid='"+contract_prjid+"' AND cttid='"+contract_ cttid+"'
AND expandid='"+contract_expandid+"'"; object objTime = BX.DAL.DBHelper.ExecuteScalar(sqlJudg
e); if(objTime != DBNull.Value) { string lstTime = Convert.ToString(objTime).Trim();
if(!lstTime.Equals(string.Empty)) ateResultXML(0,"该条数据已于"+lstTime+"成功传 入网上报销系统!\n 操作忽略!","0"); //ateResultXML(5,"该条数据已于"+lstTime+"成功 传入网上报销系统!\n 不能重复提交!","1"); }
BX.DAL.DBHelper.ExecuteNonQuery(strSQL); } catch(System.Data.SqlClient.SqlException exSQL) { //throw exSQL; ateResultXML(9,exSQL.Message,"1"); }
ateResultXML(0,"","0"); } }
}
using System; using System.Xml; using System.Data;
namespace BX.Interface { /// <summary> /// ISendAuditResult 的摘要说明。 /// </summary> public class ISendAuditResult {
/// <summary> /// 调用网上报销审批系统传递审批结果信息接口 /// </summary> /// <param name="info"></param> /// <returns>IStructResult</returns> public static string[] invoke(StructAuditR
esult v_struct) { string WSDL = System.Configuration.ConfigurationSettings.AppSettings["IS endAuditResult_WSDL"]; string SvClsName = System.Configuration.ConfigurationSettings.AppSetting s["ISendAuditResult_SvClsName"]; string WebMethod = System.Configuration.ConfigurationSettings.AppSettin gs["ISendAuditResult_WebMethod"]; string ifCode = System.Configuration.ConfigurationSettings.AppSettings["IS endAuditResult_ifCode"]; string ifPass = System.Configuration.ConfigurationSettings.AppSettings["IS endAuditResult_ifPass"]; string ifEvent = System.Configuration.ConfigurationSettings.AppSettings["I SendAuditResult_ifEvent"];
string info = creatXML(v_struct); // object[] args = new object[]{ifCode,ifPass,ifEvent,info};
//
return ICommon.SoapService(WSD
L, SvClsName, WebMethod, ifCode, args));
调用webservice服务//int GetPrjCttPayment(string ValPWD,string xmlContent,ref string ErrorInf o); object[] args = new object[]{ifPass,info,""}; int ret = int.SoapService(WSDL, SvClsName, WebMetho d, ifCode,ref args));
string[] resultInfo = new string[2]; resultInfo[0] = ret.ToString(); resultInfo[1] = Convert.ToString(args[2]).Trim();//返回错误信息 return resultInfo; }
private static string creatXML(StructAuditResult v_struct) { return xmldoc.InnerXml;}
} }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论