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.AppSettings["IReceiveContract_Id"].Trim();
            
string IReceiveContract_PSW = System.Configuration.ConfigurationSettings.AppSettings["IReceiveContract_PSW"].Trim();
            
string IReceiveNoContract_Id = System.Configuration.ConfigurationSettings.AppSettings["IReceiveNoContract_Id"].Trim();
            
string IReceiveNoContract_PSW = System.Configuration.ConfigurationSettings.AppSettings["IReceiveNoContract_PSW"].Trim();

            
if(ifCode.Equals(IReceiveContract_Id))
            
{
                
if(!ifPass.Equals(IReceiveContract_PSW))
                    
return ICommon.createResultXML(2,"","");
                
return IApplyContract.ReceiveApply(ifCode,ifInfo);
            }

            
else if(ifCode.Equals(IReceiveNoContract_Id))
            
{
                
if(!ifPass.Equals(IReceiveNoContract_PSW))
                    
return ICommon.createResultXML(2,"","");
                
return IApplyNoContract.ReceiveApply(ifCode,ifInfo);
            }

            
else
            
{            
                
return ICommon.createResultXML(1,"","");                    
            }
        
        }
 //-- end IfSerive
    }

}



// ****************************************************
//    功    能:SOAP XML WebService接口通用处理类
//    说    明:
//    创 建 者:dingzh@jstrd.com
//    创建时间: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 SvClsName, string v_WebMethod, string v_ifCode,ref object[] v_args)//不需安装SOAP ToolKit 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);   //加入using System.Web.Services.Description;   
            
//3)用服务导入器把得到的描述导入服务描述中:   
            ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();   
            sdi.AddServiceDescription(sd,
null,null);   
            
//4)指定要动态编译的代码的命名空间:   
            CodeNamespace cn = new CodeNamespace(v_NameSpace);   //"DynamicServices"
            
//5)指定要动态编译的代码单元:   
            CodeCompileUnit ccu = new CodeCompileUnit();   
            
//6)把命名空间添加到动态编译的代码单元的命名空间组中去:   
            ccu.Namespaces.Add(cn);   
            
//7)指示服务导入器把该动态编译单元内该命名空间的代码导进去,作为新的动态服务描述:   
            sdi.Import(cn,ccu);   
            
//8)新建一个C#编译器来编译:   
            ICodeCompiler icc = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();  
            
//9)新建一个编译参数对象(在这里我们设置它为空的编译参数):   
            CompilerParameters cp=new CompilerParameters();    
            
//10)指示C#编译器,按照 CP 编译参数对象提供的参数来进行 CCU 单元的编译。并把编译结果传给 编译结果对象 cr:   
            CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu);   
            
//11)从动态编译结果中获取 某个 字符串(通常是该类型名字)所表示的真实类:   
            Type t = cr.CompiledAssembly.GetType(v_ClassName);   //其中的DynamicServices必须和CodeNamespace cn = new   CodeNamespace("DynamicServices");定义的同名  
            
//12)创建该类的实例:   
            Object obj = Activator.CreateInstance(t);           
            
//13)根据方法名称字符串 反射到该方法的实体:   
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod(v_WebMethod);              
            
//14)用该方法实体的.Invoke()来调用该方法:   
            string ret = System.Convert.ToString(mi.Invoke(obj, v_args));  //,new object[]{"传给方法的参数1","传给方法的参数2"}));    
            return ret;
            
            
//            添加Web 引用,在WSDL一栏中把刚才得到的WSDL地址复制过来,Web 引用的名称输入JavaService
            
//            JavaService.ServiceService bean = new JavaService.ServiceService();
            
//            string aa = bean.IfService("ifUser","ifPass","ifCode","ifEvent","info");
        }
  //-- End getSoapService/



        
/// <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_ifResultInfo)
        
{
            
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");//查找<IfResponse> 

            XmlElement eIfResult 
= xmldoc.CreateElement("IfResult");//创建一个<IfResult>节点                 
            eIfResult.SetAttribute("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("data.xml") ) ; 

            
return xmldoc.InnerXml;
            
//xmldoc.OuterXml;

//        <?xml version="1.0" encoding="GB2312"?>
//        <IfResponse>
//        <IfResult>接口结果代码</IfResult>
//        <IfError>错误信息</IfError>
//        <IfErrorStack>错误信息堆栈</IfErrorStack>
//        <IfResultInfo>接口结果信息</IfResultInfo>
//        </IfResponse>
//
//        <IfResult/>依照下述约定:
//        成功:0 —仅表示接口调用成功,并不代表业务调用成功,业务返回结果信息存放于<IfResultInfo/>节点中
//        失败:非零整数值    
//            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(System.IO.StringReader sr = new System.IO.StringReader(xml))
            
{            
                ds.ReadXml(sr);
            }


//            //方法二
//            using(System.Xml.XmlReader xr = System.Xml.XmlReader.Create(new System.IO.StringReader(xml)))
//            {
//                ds.ReadXml(xr);
//            }

//            //方法三
//            using(System.IO.Stream st = new System.IO.MemoryStream(System.Text.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).InnerText.Trim();
                structResult.IfResult        
= strResult.Equals(string.Empty)?-1:int.Parse(strResult);
                structResult.IfError        
= xmldoc.GetElementsByTagName("IfError").Item(0).InnerText.Trim();
                structResult.IfErrorStack    
= xmldoc.GetElementsByTagName("IfErrorStack").Item(0).InnerText.Trim();
                structResult.IfResultInfo    
= xmldoc.GetElementsByTagName("IfResultInfo").Item(0).InnerText.Trim();
            }

            
catch(Exception e)
            
{
                structResult.IfResult 
= -1;
                structResult.IfError 
= "未能处理的异常返回结果";
                structResult.IfErrorStack 
= e.Message;
                
return structResult;
                
//throw e;                            
            }

            
return structResult;
        }

    }



    
/// <summary>
    
/// 接口返回结果结构体
    
/// </summary>

    public struct IStructResult
    
{
        
public int        IfResult;
        
public string    IfError;
        
public string    IfErrorStack;
        
public string    IfResultInfo;

        
public void Clear()
        
{
            IfResult        
= -1;
            IfError            
= "";
            IfErrorStack    
= "";
            IfResultInfo    
= "";
        }

    }

}



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;
                return ICommon.createResultXML(3,ex.Message,"");                
            }


try
            
{
                action 
= xmldoc.GetElementsByTagName("action").Item(0).InnerText.Trim();
.
.
//XML节点解析//
.
.
}

            
catch(Exception e)
            
{
                
//throw e;
                return ICommon.createResultXML(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(sqlJudge);
                
if(objTime != DBNull.Value)
                
{
                    
string lstTime = Convert.ToString(objTime).Trim();
                    
if(!lstTime.Equals(string.Empty))
                        
return ICommon.createResultXML(0,"该条数据已于"+lstTime+"成功传入网上报销系统!\n操作忽略!","0");
                        
//return ICommon.createResultXML(5,"该条数据已于"+lstTime+"成功传入网上报销系统!\n不能重复提交!","1");
                }


                BX.DAL.DBHelper.ExecuteNonQuery(strSQL);
            }
 
            
catch(System.Data.SqlClient.SqlException exSQL)
            
{
                
//throw exSQL;
                return ICommon.createResultXML(9,exSQL.Message,"1");                
            }


            
return ICommon.createResultXML(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(StructAuditResult v_struct)
        
{
            
string WSDL = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_WSDL"];
            
string SvClsName = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_SvClsName"];
            
string WebMethod = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_WebMethod"];
            
string ifCode = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_ifCode"];
            
string ifPass = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_ifPass"];            
            
string ifEvent = System.Configuration.ConfigurationSettings.AppSettings["ISendAuditResult_ifEvent"];    
        
            
string info = creatXML(v_struct);
//            object[] args = new object[]{ifCode,ifPass,ifEvent,info};        
            
//            return ICommon.parseResultXML2Struct(ICommon.getSoapService(WSDL, SvClsName, WebMethod, ifCode, args));

            
//int GetPrjCttPayment(string ValPWD,string xmlContent,ref string ErrorInfo);            
            object[] args = new object[]{ifPass,info,""};
            
int ret = int.Parse(ICommon.getSoapService(WSDL, SvClsName, WebMethod, 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;}

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值