当前位置:编程学习 > C#/ASP.NET >>

动态调用webservice ,怎样soapheader身份认证。

下面是相关的代码,如果您做过相关的,帮忙加Q吧1347895284,
现在还有二百分,都给您。麻烦了。
赋值下面的代码,然后直接搜索  zhang add  ,
这这部分里是我自己写的认证方法,反射存在问题。字数原因代码不全,见谅

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Services.Description;
using System.Xml.Serialization;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Web.Services.Protocols;

namespace CZH.Util
{
    /// <summary>
    ///动态调用WebService
    /// </summary>
    public class WebServiceUtil
    {
        public static object CallWebService(string WebServiceURL, string AgentClassName, string MethodName, object[] Parameters, string Timeout, string DllPath, out string ErrorMsg)
        {
            object Result = null;
            ErrorMsg = "";
            try
            {
                if (WebServiceURL == null || WebServiceURL == "")
                {
                    ErrorMsg = "WebServiceURL不能为空!";
                    return null;
                }
                if (MethodName == null || MethodName == "")
                {
                    ErrorMsg = "MethodName不能为空!";
                    return null;
                }
                //if (DllName == null || DllName == "")
                //{
                //    ErrorMsg = "DllName不能为空!";
                //    return null;
                //}
                if (DllPath == null || DllPath == "")
                {
                    ErrorMsg = "DllPath不能为空!";
                    return null;
                }

                if (Timeout == null || Timeout == "")
                {
                    Timeout = "60000";//默认为1分钟
                }
        private static object ExecMethod(Assembly asm, string WebServiceURL, string AgentClassName, string MethodName, object[] Parameters, int Timeout, string DllName, string DllPath, bool IsUpdWebService, out string ErrorMsg)
        {
            ErrorMsg = "";
            object Result = null;
            Type t = asm.GetType("mydynamiccallwebservice." + AgentClassName);

            #region   zhang add  动态加soap头
            //Soap头开始  
            FieldInfo client = t.GetField("ClientKey");
            //获取客户端验证对象  
            Type typeClient = asm.GetType("mydynamiccallwebservice" + ".ClientKey");
            FieldInfo userName = typeClient.GetField("UserName");
            FieldInfo passWord = typeClient.GetField("PassWord");
            //为验证对象赋值  
            object clientkey = Activator.CreateInstance(typeClient);
            userName.SetValue(clientkey,"test"); //???
            passWord.SetValue(clientkey,"test"); //???
            //Soap头结束  
            #endregion

            object o = Activator.CreateInstance(t);
            //zhang add
            client.SetValue(o, clientkey);
            //获取方法  
            MethodInfo method = t.GetMethod(MethodName);//GetPersons是服务端的方法名称

            if (method == null)
            {
                if (IsUpdWebService == true)//动态更新WebService
                {
                    //  File.Delete(DllPath);//先删除此dll
                    string TempStr = DllName.Substring(DllName.LastIndexOf("_") + 1);
                    TempStr = TempStr.Substring(0, TempStr.LastIndexOf(".dll"));
                    int DllMaxNum = Convert.ToInt32(TempStr);

                    //给10次机会生成dll,如果还是调用不到的话,那么就代表这“方法名”在WebService里面已去除了
                    if (DllMaxNum == 10)
                    {
                        ErrorMsg = "没有此方法:" + method + ",请重新设置方法名;注意:请把此文件夹:" + DllPath + " 下面自动生成dll都删除,如果删除不了,先停了这些dll所在的宿主(比如IIS,或者当前的应用程序等)再删除此dll,不然有可能是被这宿主占用了资源删除不了!";
                        return null;
                    }

                    DllName = "webservice_dynamiccall_" + (++DllMaxNum).ToString() + ".dll";//原来的那个dll没法删除,因为占用了资源,没法释放资源,只有垃圾回收站释放资源了,但是又得要更新些Dll,所以重新生成另一个新的dll


                    //如果method为null,那么只有两种可能:1.此类下没有此方法  2.服务器那边WebSrvice添加了新的方法,此代理类没有更新
                    //所以这里处理是:第一次method为null,那么给一次机会,用下面的方法"OutputAssembly"重新更新下WebService,并重新生成dll文件
                    OutputAssembly(WebServiceURL, Timeout, DllName, DllPath, out ErrorMsg);
                    if (ErrorMsg != "")//代表出错了
                    {
                        return null;
                    }

                    //重新加载新的dll
                    asm = Assembly.LoadFrom(DllPath + DllName);
                    t = asm.GetType("mydynamiccallwebservice." + AgentClassName);
                    o = Activator.CreateInstance(t);
                    method = t.GetMethod(MethodName);

                }
                //如果method还为null,那么则"MethodName"则真是错误了
                if (method == null)
                {
                    ErrorMsg = "没有此方法: " + MethodName;
                    return null;
                }
            }

            try
            {
                //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了
                Result = method.Invoke(o, Parameters);
            }
            catch (Exception e)
            {

                ErrorMsg = "参数有错误:Parameters" + e.ToString(); ;
            }
            return Result;
        }


        private static void OutputAssembly(string WebServiceURL, int Timeout, string DllName, string DllPath, out string ErrorMsg)
        {
            ErrorMsg = "";
            try
            {

                HttpWebRequest client = (HttpWebRequest)HttpWebRequest.Create(WebServiceURL);
                client.Timeout = Timeout;
                

                Stream stream = ((HttpWebResponse)client.GetResponse()).GetResponseStream();

                ServiceDescription description = ServiceDescription.Read(stream);

                ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。
                

                importer.ProtocolName = "Soap"; //指定访问协议。
                importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。
                importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

                importer.AddServiceDescription(description, null, null); //添加WSDL文档。

                CodeNamespace nmspace = new CodeNamespace(); //命名空间
                nmspace.Name = "mydynamiccallwebservice";

                CodeCompileUnit unit = new CodeCompileUnit();
                unit.Namespaces.Add(nmspace);

                ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
                CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

                CompilerParameters parameter = new CompilerParameters();
                parameter.GenerateExecutable = false;
                parameter.OutputAssembly = DllName;//输出程序集的名称
                parameter.ReferencedAssemblies.Add("System.dll");
                parameter.ReferencedAssemblies.Add("System.XML.dll");
                parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
                parameter.ReferencedAssemblies.Add("System.Data.dll");

                CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
                if (result.Errors.HasErrors)
                {
                    // 显示编译错误信息
                    ErrorMsg = "动态编绎:\"" + DllName + "\"有错误!";
                }
                //这里就生成了动态的dll
                File.Copy(AppDomain.CurrentDomain.BaseDirectory + DllName, DllPath + DllName);
                File.Delete(AppDomain.CurrentDomain.BaseDirectory + DllName);
            }
            catch (Exception ex)
            {
                ErrorMsg = "创建代理类并动态生成Dll出错:" + ex.Message + "; " + ex.StackTrace;
            }
        }



    }
}
webservice  soapheader 动态调用 --------------------编程问答-------------------- 顶一个,求解
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,