【超细完整版】C# 获取WebService所有方法并调用 【调用篇】

本文主要是介绍【超细完整版】C# 获取WebService所有方法并调用 【调用篇】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

注意:该文章涉及到的调用方法若找不到 请移步第一部分内容查找


C# 生成wsdl和dll教程请移步
【超细完整版】C# WebService 通过URL生成WSDL文件和DLL文件> 【生成篇】


开始

首先实现一个类,用于实现对URL的验证等

public class InputFormatVerification
{/// <summary>/// 是否合法Url地址(统一资源定位)/// </summary>/// <param name="strValue">url地址</param>/// <returns>成功返回true 失败返回false</returns>public static bool IsUrl(string strValue){string RegexStr = string.Empty;RegexStr = @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$";return formatChecks(RegexStr, strValue);}/// <summary>/// 检测串值是否为合法的格式/// </summary>/// <param name="strRegex">正则表达式</param>/// <param name="strValue">要检测的String值</param>/// <returns>成功返回true 失败返回false</returns>public static bool formatChecks(string strRegex, string strValue){if (string.IsNullOrWhiteSpace(strValue)){return false;}Regex re = new Regex(strRegex);return re.IsMatch(strValue);}
}

实现Invoke

获取WebService中的所有web 方法
#region 获取web方法
/// <summary>
/// 获取WebService接口的所有WebMethod方法
/// 通过WebService方法的特性为【System.Web.Services.Protocols.SoapDocumentMethodAttribute】
/// 根据特性SoapDocumentMethodAttribute来筛选出所有WebMethod方法
/// </summary>
/// <param name="url"></param>
public static List<MethodInfo> GetAllWebMethodsFromLink(string url, out string className)
{className = GetClassNameFromUrl(url);CompilerResults result = UrlToDllFile(url);Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);return GetWebMethods(type);
}
/// <summary>
/// 获取WebService接口的所有WebMethod方法
/// 通过WebService方法的特性为【System.Web.Services.Protocols.SoapDocumentMethodAttribute】
/// 根据特性SoapDocumentMethodAttribute来筛选出所有WebMethod方法
/// </summary>
/// <param name="wsdlFilePath"></param>
public static List<MethodInfo> GetAllWebMethodsFromWsdl(string wsdlFilePath, out string className)
{className = GetClassNameFromWsdl(wsdlFilePath);CompilerResults result = WsdlToDll(wsdlFilePath);Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);return GetWebMethods(type);
}private static List<MethodInfo> GetWebMethods(Type type)
{List<MethodInfo> methodInfoList = new List<MethodInfo>();if (type == null){return methodInfoList;}MethodInfo[] methodInfos = type.GetMethods();for (int i = 0; i < methodInfos.Length; i++){MethodInfo methodInfo = methodInfos[i];//WebMethod方法的特性为:System.Web.Services.Protocols.SoapDocumentMethodAttribute Attribute attribute = methodInfo.GetCustomAttribute(typeof(System.Web.Services.Protocols.SoapDocumentMethodAttribute));if (methodInfo.MemberType == MemberTypes.Method && attribute != null){methodInfoList.Add(methodInfo);}}return methodInfoList;
}
#endregion
通过wsdl或url进行调用
/// <summary>
/// 调用WebService
/// </summary>
/// <param name="address">WebService地址</param>
/// <param name="methodName">方法名称</param>
/// <param name="args">参数列表</param>
/// <param name="timeOut"></param>
/// <returns>返回调用结果</returns>
/// <exception cref="Exception"></exception>
private static object InvokeWebService(string address, string methodName, object[] args, string timeOut = "")
{try{string className = string.Empty;CompilerResults result = null;//支持直接URL或wsdl类型文件的调用if (InputFormatVerification.IsUrl(address)){className = GetClassNameFromUrl(address);result = UrlToDllFile(address);}else{className = GetClassNameFromWsdl(address);result = WsdlToDll(address);}Assembly assembly = result.CompiledAssembly;Type type = assembly.GetType(className);FieldInfo[] arry = type.GetFields();//实例类型对象   object obj = Activator.CreateInstance(type);System.Reflection.MethodInfo mi = type.GetMethod(methodName);//添加超时时间if (!string.IsNullOrEmpty(timeOut)){int timeout = 0;int.TryParse(timeOut, out timeout);if (timeout == 0) timeout = 1200;//设置超时时间((System.Web.Services.Protocols.WebClientProtocol)(obj)).Timeout = timeout * 1000;//毫秒s,timeOut超时时间设置为分钟}var res = mi.Invoke(obj, args);return res;}catch (Exception ex){throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));}
}

老规矩!😄

这篇关于【超细完整版】C# 获取WebService所有方法并调用 【调用篇】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826038

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

5分钟获取deepseek api并搭建简易问答应用

《5分钟获取deepseekapi并搭建简易问答应用》本文主要介绍了5分钟获取deepseekapi并搭建简易问答应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需... 目录1、获取api2、获取base_url和chat_model3、配置模型参数方法一:终端中临时将加

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

四种Flutter子页面向父组件传递数据的方法介绍

《四种Flutter子页面向父组件传递数据的方法介绍》在Flutter中,如果父组件需要调用子组件的方法,可以通过常用的四种方式实现,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录方法 1:使用 GlobalKey 和 State 调用子组件方法方法 2:通过回调函数(Callb

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

Java中Object类的常用方法小结

《Java中Object类的常用方法小结》JavaObject类是所有类的父类,位于java.lang包中,本文为大家整理了一些Object类的常用方法,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. public boolean equals(Object obj)2. public int ha

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的