客户端代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.CodeDom.Compiler; using System.CodeDom; using System.Web.Services.Description; using System.Net; using System.IO; using System.Reflection; using Microsoft.CSharp; using System.Web.Configuration; using System.Xml.Serialization; using System.Xml; using System.Globalization; using System.Security.Permissions; namespace WebServiceTest { /// <summary> /// 动态调用WebService(支持SaopHeader) /// </summary> public class WebServiceHelper { /// <summary> /// 获取WebService的类名 /// </summary> /// <param name="wsUrl">WebService地址</param> /// <returns>返回WebService的类名</returns> private static string GetWsClassName(string wsUrl) { string[] parts = wsUrl.Split('/'); string[] pps = parts[parts.Length - 1].Split('.'); return pps[0]; } /// <summary> /// 调用WebService(不带SoapHeader) /// </summary> /// <param name="wsUrl">WebService地址</param> /// <param name="methodName">方法名称</param> /// <param name="args">参数列表</param> /// <returns>返回调用结果</returns> public static object InvokeWebService(string wsUrl, string methodName, object[] args) { return InvokeWebService(wsUrl, null, methodName, null, args); } /// <summary> /// 调用WebService(带SoapHeader) /// </summary> /// <param name="wsUrl">WebService地址</param> /// <param name="methodName">方法名称</param> /// <param name="soapHeader">SOAP头</param> /// <param name="args">参数列表</param> /// <returns>返回调用结果</returns> public static object InvokeWebService(string wsUrl, string methodName, SoapHeader soapHeader, object[] args) { return InvokeWebService(wsUrl, null, methodName, soapHeader, args); } /// <summary> /// 调用WebService /// </summary> /// <param name="wsUrl">WebService地址</param> /// <param name="className">类名</param> /// <param name="methodName">方法名称</param> /// <param name="soapHeader">SOAP头</param> /// <param name="args">参数列表</param> /// <returns>返回调用结果</returns> public static object InvokeWebService(string wsUrl, string className, string methodName, SoapHeader soapHeader, object[] args) { string @namespace = "IDPSystem.WebUI.WebService"; if ((className == null) || (className == "")) { className = GetWsClassName(wsUrl); } try { //获取WSDL WebClient wc = new WebClient(); Stream stream = wc.OpenRead(wsUrl + "?wsdl"); ServiceDescription sd = ServiceDescription.Read(stream); ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.Style = ServiceDescriptionImportStyle.Client;//生成客户端代理 sdi.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.None | System.Xml.Serialization.CodeGenerationOptions.GenerateOldAsync | System.Xml.Serialization.CodeGenerationOptions.GenerateOrder; sdi.ProtocolName = "Soap12"; sdi.AddServiceDescription(sd, "", ""); CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码 CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); //CSharpCodeProvider csc = new CSharpCodeProvider(); //ICodeCompiler icc = csc.CreateCompiler(); CodeDomProvider icc = CodeDomProvider.CreateProvider("CSharp"); // icc.GenerateCodeFromCompileUnit(ccu, Console.Out, new CodeGeneratorOptions());控制台输出 //设定编译参数 CompilerParameters cplist = new CompilerParameters(); cplist.GenerateExecutable = false; cplist.GenerateInMemory = true; cplist.OutputAssembly = @namespace + ".dll";//输出程序集的名称 cplist.ReferencedAssemblies.Add("System.dll"); cplist.ReferencedAssemblies.Add("System.XML.dll"); cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); cplist.ReferencedAssemblies.Add("System.Data.dll"); cplist.ReferencedAssemblies.Add("System.ServiceModel.dll"); cplist.IncludeDebugInformation = true; //编译代理类 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu); if (true == cr.Errors.HasErrors) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors) { sb.Append(ce.ToString()); sb.Append(System.Environment.NewLine); } throw new Exception(sb.ToString()); } //上面是根据WebService地址,模似生成一个代理类,如果你想看看生成的代码文件是什么样子,可以用以下代码保存下来,默认是保存在bin目录下面 TextWriter writer = File.CreateText("MyTest3.cs"); icc.GenerateCodeFromCompileUnit(ccu, writer, null); writer.Flush(); writer.Close(); //生成代理实例,并调用方法 System.Reflection.Assembly assembly = cr.CompiledAssembly; Type t = assembly.GetType(@namespace + "." + className, true, true); FieldInfo[] arry = t.GetFields(); FieldInfo client = null; // PropertyInfo pro = null; object clientkey = null; if (soapHeader != null) { //Soap头开始 client = t.GetField(soapHeader.ClassName + "Value"); //获取客户端验证对象 soap类 Type typeClient = assembly.GetType(@namespace + "." + soapHeader.ClassName); //为验证对象赋值 soap实例 clientkey = Activator.CreateInstance(typeClient); //遍历属性 foreach (KeyValuePair<string, object> property in soapHeader.Properties) { typeClient.GetField(property.Key).SetValue(clientkey, property.Value); // typeClient.GetProperty(property.Key).SetValue(clientkey, property.Value, null); } //Soap头结束 } //实例类型对象 object obj = Activator.CreateInstance(t); if (soapHeader != null) { //设置Soap头 client.SetValue(obj, clientkey); //pro.SetValue(obj, soapHeader, null); } ///作为object传递 List<object> objlist = new List<object>(); //代理类中staff 类型 Type ProxyStaff = assembly.GetType("IDPSystem.WebUI.WebService.Entity"); ///转为数组 Type ProxyStaffArray = ProxyStaff.MakeArrayType(1); SameWSEntity[] wsEntityList = (SameWSEntity[])args[(args.Length - 1)]; MethodInfo[] melist = ProxyStaffArray.GetMethods(BindingFlags.Public | BindingFlags.Instance); object array = ProxyStaffArray.InvokeMember("Set", BindingFlags.CreateInstance, null, wsEntityList, new object[] { wsEntityList.Length }); int i = 0;//array foreach (SameWSEntity staff in wsEntityList) { //创建代理类中staff的实例 object ProxyStaffEntity = Activator.CreateInstance(ProxyStaff); //遍历属性 foreach (FieldInfo item in ProxyStaff.GetFields()) { //遍历时候给属性赋值 object objVal = typeof(SameWSEntity).GetProperty(item.Name).GetValue(staff, null); item.SetValue(ProxyStaffEntity, objVal); } MethodInfo[] list = ProxyStaffArray.GetMethods(); ProxyStaffArray.GetMethod("SetValue", new Type[] { typeof(object), typeof(int) }).Invoke(array, new object[] { ProxyStaffEntity, i }); i++; //作为object数组 objlist.Add(ProxyStaffEntity); } //作为object数组 args[args.Length - 1] = array; // objlist.ToArray(); System.Reflection.MethodInfo mi = t.GetMethod(methodName); return mi.Invoke(obj, args); } catch (Exception ex) { throw new Exception(ex.ToString()); } } //慢慢理解吧,懒得写了 private static ErrorMessage ConvertToErrMsg(object obj) { ErrorMessage errMsg = new ErrorMessage(); List<ErrorMessage.ErrorData> errdata = new List<ErrorMessage.ErrorData>(); Type t = obj.GetType(); foreach (FieldInfo item in t.GetFields()) { Type tlist = item.FieldType; if (tlist.IsArray) { object[] val = item.GetValue(obj) as object[]; Type errType = val.GetType(); object array = errType.InvokeMember("Set", BindingFlags.CreateInstance, null, val, new object[] { val.Length }); MethodInfo method = errType.GetMethod("GetValue", new Type[] { typeof(int) }); for (int i = 0; i < val.Length; i++) { ErrorMessage.ErrorData err = new ErrorMessage.ErrorData(); object val2 = method.Invoke(val, new object[] { 0 }); Type errWS = val2.GetType(); foreach (FieldInfo field in errWS.GetFields()) { switch (field.Name) { case "ID": err.ID = field.GetValue(val2).ToString(); break; case "Message": err.Message = field.GetValue(val2).ToString(); break; } } errdata.Add(err); } } else { object val = item.GetValue(obj); switch (item.Name) { case "ErrorCode": errMsg.ErrorCode = Convert.ToInt32(val); break; case "ErrorMsg": errMsg.ErrorMsg = val.ToString(); break; } } } errMsg.ErrorList = errdata.ToArray(); return errMsg; } /// <summary> /// SOAP头 /// </summary> public class SoapHeader { /// <summary> /// 构造一个SOAP头 /// </summary> public SoapHeader() { this.Properties = new Dictionary<string, object>(); } /// <summary> /// 构造一个SOAP头 /// </summary> /// <param name="className">SOAP头的类名</param> public SoapHeader(string className) { this.ClassName = className; this.Properties = new Dictionary<string, object>(); } /// <summary> /// 构造一个SOAP头 /// </summary> /// <param name="className">SOAP头的类名</param> /// <param name="properties">SOAP头的类属性名及属性值</param> public SoapHeader(string className, Dictionary<string, object> properties) { this.ClassName = className; this.Properties = properties; } /// <summary> /// SOAP头的类名 /// </summary> public string ClassName { get; set; } /// <summary> /// SOAP头的类属性名及属性值 /// </summary> public Dictionary<string, object> Properties { get; set; } /// <summary> /// 为SOAP头增加一个属性及值 /// </summary> /// <param name="name">SOAP头的类属性名</param> /// <param name="value">SOAP头的类属性值</param> public void AddProperty(string name, object value) { if (this.Properties == null) { this.Properties = new Dictionary<string, object>(); } Properties.Add(name, value); } } } }
-------------------------------------------------------------------
服务器端方法和对象
对象:
using System; using System.Web.Services.Protocols; using IDPSystem.BLL; using IDPSystem.BLL.DictionaryEnum.StaffRole; namespace IDPSystem.WebUI.WebService { /// <summary> /// 用户认证用的数据 /// </summary> public class Certificate : SoapHeader { public Certificate() { } /// <summary> /// 用户名 /// </summary> public string LoginUserName { get; set; } /// <summary> /// 密码 /// </summary> public string LoginPassword { get; set; } } /// <summary> /// WebService使用的数据 /// </summary> [Serializable] public class Staff { #region 属性 /// <summary> /// 员工ID /// </summary> public string Identity { get; set; } /// <summary> /// 员工编号 /// </summary> public string Number { get; set; } /// <summary> /// 员工姓名 /// </summary> public string Name { get; set; } /// <summary> /// 离职状态 /// </summary> public bool Dimission { get; set; } /// <summary> /// 数据最后一次更新用户 /// </summary> internal string LastUpdateUserInfoID { get; set; } /// <summary> /// 登陆用户信息 /// </summary> internal string UserInfoIdentity { get; set; } /// <summary> /// 公司ID /// </summary> internal string CorparionID { get; set; } #endregion #region 构造函数 public Staff() { this.UpdateTime = DateTime.Now.ToString(); this.InsertTime = DateTime.Now.ToString(); } /// <summary> /// WebService返回值 /// </summary> public class WS_ErrorMessage { public struct ErrorData { public string ID; public string Message; public ErrorData(string id, string msg) { this.ID = id; this.Message = msg; } } /// <summary> /// 返回错误信息结果集 /// </summary> private ErrorData[] errorList; public ErrorData[] ErrorList { get { return errorList; } set { errorList = value; } } /// <summary> /// 错误码 0成功 1失败 2部分成功 /// </summary> private int errorCode; public int ErrorCode { get { return errorCode; } set { errorCode = value; } } /// <summary> /// 错误信息 /// </summary> private string errorMsg; public string ErrorMsg { get { return errorMsg; } set { errorMsg = value; } } } }
方法:
[XmlInclude(typeof(Staff[])), XmlInclude(typeof(ErrorMessage)), XmlInclude(typeof(IDPSystem.WebUI.WebService.WS_ErrorMessage.ErrorData))] public class WS_StaffOperate : System.Web.Services.WebService { /// <summary> /// 验证权限 /// </summary> public Certificate certificate = new Certificate(); [SoapHeader("certificate")] [WebMethod] public WS_ErrorMessage HelloWorld(Staff staff) { } }
--------------------------------------
客户端对象:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WebServiceTest { public class ErrorMessage { public class ErrorData { public string ID; public string Message; public ErrorData(string id, string msg) { this.ID = id; this.Message = msg; } public ErrorData() { } } /// <summary> /// 返回错误信息结果集 /// </summary> private ErrorData[] errorList; public ErrorData[] ErrorList { get { return errorList; } set { errorList = value; } } /// <summary> /// 错误码 0成功 1失败 2部分成功 /// </summary> private int errorCode; public int ErrorCode { get { return errorCode; } set { errorCode = value; } } /// <summary> /// 错误信息 /// </summary> private string errorMsg; public string ErrorMsg { get { return errorMsg; } set { errorMsg = value; } } } }
相关推荐
.Net动态调用WebService .Net动态调用WebService .Net动态调用WebService
在不改变程序引用的情况下 动态更改WebService的地址
C#_.NET_动态调用webservice的三种方式
.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子.net调用webservice接口例子
asp.net调用java webservice实例
C# .NET 动态调用webservice的三种方式.pdfC# .NET 动态调用webservice的三种方式.pdf
C#.NET动态调用webservice的三种方式.pdfC#.NET动态调用webservice的三种方式.pdf
第一次做 .net来动态调用 php web服务。内含nusoap 写的一个简单php服务端,客户端采用.net写的一个动态调用类 相关帖子 http://topic.csdn.net/u/20090819/19/94f3754b-7635-4377-8dc4-08cab4cd1aad.html?87815 ...
c#.net动态调用webservice的三种方式.docx
C#.NET动态调用webservice的三种方式.doc
在ASP.NET中调用基于HTTP基本认证的WebService接口
delphi调用asp.net中的WebService 实例 原代码可以运行
自己写的WebService分析工具,可以根据Webservice地址获得所有方法的名称、参数以及返回值类型和方法描述信息,另支持动态调用
Java调用.NET的WebService接口实例,jar包删减到三个,干净清爽,里面包含注多注释和图解,搞了半天的家伙拿出来与大家分享,无任何BUG,修改里面的参数即可直接运行,对于这种好东西,花了半天时间10分不算高,重在...
在Asp.net中利用ScriptManager调用webservice; vs2008+C#环境中编译通过,且能正常运行的例子。Framework 3.5
利用公共webservice实现天气预报功能。 其目的是:为了学习如何调用webservice。 其中利用了无刷新技术,提高用户体验。适合与像本人这样的webservice初用者。
多数时候我们通过 "添加 Web 引用..." 创建客户端代理类的方式调用WebService,但在某些情况下我们可能需要在程序运行期间动态调用一个未知的服务。本文主要介绍了动态调用webservice的三种方式。
调用webservice接口,利用post方式传参数,将返回的XML结果,利用xmldocument返回到页面中。
asp.net邮件发送webservice,无组件,直接可以用
主要介绍了Asp.NET Core 如何调用WebService的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧