【超细完整版】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

相关文章

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔