欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > c# >内容正文

c#

c#开发xml webservice接口(soap) -尊龙游戏旗舰厅官网

发布时间:2024/10/8 c# 0 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 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
{
   
/**//// 


    
/// service1 的摘要说明。
    
/// 
 
    public class doservice : system.web.services.webservice
    
{

/**//// 
        
/// 网上报销系统webservice服务通用调用方法
        
/// 

        
/// 接口标识
        
/// 校验码
        
/// 操作方式(可空)
        
/// 传入的xml信息
        
///  

        [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
    }

}

 

通用处理类icommon.cs
// ****************************************************
//    功    能: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
{
    
/**//// 


    
/// soap xml webservice接口通用处理类
    
/// 
 
    public class icommon
    
{
        
public icommon()
        
{
            
//
            
// todo: 在此处添加构造函数逻辑
            
//
        }
        
            
        
/**//// 
        
/// 动态引用一个xml web service        
        
/// ifserive(string ifcode,string ifpass,string ifevent,string ifinfo)
        
/// 

        
/// wsdl路径
        
/// 接口服务真实类名
        
/// 接口函数方法名
        
/// 接口标识
        
/// 接口函数参数列表数组
        
/// string 

        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/



        
/**//// 
        
/// 创建返回结果xml
        
/// 

        
/// 接口结果代码
        
/// 错误信息堆栈
        
/// 接口结果信息
        
/// string         

        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的声明段落,
            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");//查找 

            xmlelement eifresult 
= xmldoc.createelement("ifresult");//创建一个节点                 
            eifresult.setattribute("note","接口结果代码"); //设置该节点note属性 
            eifresult.innertext = v_ifresult.tostring(); //设置节点文本值
            root.appendchild(eifresult);  //添加到节点中 

            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;

//        
//        
//        接口结果代码
//        错误信息
//        错误信息堆栈
//        接口结果信息
//        
//
//        依照下述约定:
//        成功:0 —仅表示接口调用成功,并不代表业务调用成功,业务返回结果信息存放于节点中
//        失败:非零整数值    
//            1:无此接口标识ifcode
//            2:调用系统校验未通过    
//            3:传入的不是一个正确的xml格式
//            4:info参数中的字段不符合规约中的要求
//            5:该条数据已重复发送
//            9:本系统处理时发生异常
        }
 //-- end createresultxml/

        

        
/**//// 
        
/// 将xml格式字符串转化成dataset
        
/// 

        
/// xml字符串
        
/// dataset 

        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;
        }


        
/**//// 
        
/// 将xml格式字符串转化成istructresult
        
/// 

        
/// xml字符串
        
/// istructresult 

        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;
        }

    }



    
/**//// 
    
/// 接口返回结果结构体
    
/// 
 

    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
{
    
/**//// 


    
/// iapplycontract 的摘要说明。
    
/// 
 
    public class iapplycontract
    
{
        
public iapplycontract()
        
{
            
//
            
// todo: 在此处添加构造函数逻辑
            
//
        }
        

        
/**//// 
        
/// 接收合同付款申请信息
        
/// 

        
/// 
        
/// 

        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
{
    
/**//// 
    
/// isendauditresult 的摘要说明。
    
/// 

    public class isendauditresult
    
{

    
/**//// 
        
/// 调用网上报销审批系统传递审批结果信息接口        
        
/// 

        
/// 
        
/// istructresult

        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;}

   }

}

转载于:https://www.cnblogs.com/fery/archive/2010/08/01/1790094.html

总结

以上是尊龙游戏旗舰厅官网为你收集整理的c#开发xml webservice接口(soap)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图