博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.net动态调用WebService
阅读量:6294 次
发布时间:2019-06-22

本文共 3383 字,大约阅读时间需要 11 分钟。

Public Class WebServiceHelper#Region "InvokeWebService"    '动态调用web服务    Public Shared Function InvokeWebService(ByVal url As String, ByVal methodname As String, ByVal args As Object()) As Object        Return WebServiceHelper.InvokeWebService(url, Nothing, methodname, args)    End Function    Public Shared Function InvokeWebService(ByVal url As String, ByVal classname As String, ByVal methodname As String, ByVal args As Object()) As Object        Dim [namespace] As String = "EnterpriseServerBase.WebService.DynamicWebCalling"        If (classname Is Nothing) OrElse (classname = "") Then            classname = WebServiceHelper.GetWsClassName(url.ToLower.Replace("?wsdl", ""))        End If        Try            If url.ToLower.IndexOf("?wsdl") = -1 Then                url &= "?wsdl"            End If            '获取WSDL            Dim wc As New WebClient()            Dim stream As Stream = wc.OpenRead(url)            Dim sd As ServiceDescription = ServiceDescription.Read(stream)            Dim sdi As New ServiceDescriptionImporter()            sdi.AddServiceDescription(sd, "", "")            Dim cn As New CodeNamespace([namespace])            '生成客户端代理类代码            Dim ccu As New CodeCompileUnit()            ccu.Namespaces.Add(cn)            sdi.Import(cn, ccu)            '设定编译参数            Dim cplist As New CompilerParameters()            cplist.GenerateExecutable = False            cplist.GenerateInMemory = True            cplist.ReferencedAssemblies.Add("System.dll")            cplist.ReferencedAssemblies.Add("System.XML.dll")            cplist.ReferencedAssemblies.Add("System.Web.Services.dll")            cplist.ReferencedAssemblies.Add("System.Data.dll")            '编译代理类            Dim vbc As New VBCodeProvider            Dim cr As CompilerResults = vbc.CompileAssemblyFromDom(cplist, ccu)            If True = cr.Errors.HasErrors Then                Dim sb As New System.Text.StringBuilder()                For Each ce As System.CodeDom.Compiler.CompilerError In cr.Errors                    sb.Append(ce.ToString())                    sb.Append(System.Environment.NewLine)                Next                Throw New Exception(sb.ToString())            End If            '生成代理实例,并调用方法            Dim assembly As System.Reflection.Assembly = cr.CompiledAssembly            Dim t As Type = assembly.[GetType]([namespace] & "." & classname, True, True)            Dim obj As Object = Activator.CreateInstance(t)            Dim mi As System.Reflection.MethodInfo = t.GetMethod(methodname)            Return mi.Invoke(obj, args)        Catch ex As Exception            Throw New Exception(ex.InnerException.Message, New Exception(ex.InnerException.StackTrace))        End Try    End Function#End Region    Private Shared Function GetWsClassName(ByVal wsUrl As String) As String        Dim parts As String() = wsUrl.Split("/"c)        Dim pps As String() = parts(parts.Length - 1).Split("."c)        Return pps(0)    End FunctionEnd Class

调用方法:

Public Function SyncResume(ByVal ResumeID As Integer) As String        Dim sResult As String = ""        Dim url As String = "http://www.webservicex.net/globalweather.asmx"        Dim args As Object() = New Object() {
"beijing","China"} sResult = WebServiceHelper.InvokeWebService(url, "GetWeather", args) Return sResult End Function

 

引用自:

转载于:https://www.cnblogs.com/icycore/p/3623059.html

你可能感兴趣的文章
PHP导出table为xls出现乱码解决方法
查看>>
PHP问题 —— 丢失SESSION
查看>>
Java中Object类的equals()和hashCode()方法深入解析
查看>>
数据库
查看>>
dojo.mixin(混合进)、dojo.extend、dojo.declare
查看>>
Python 数据类型
查看>>
iOS--环信集成并修改头像和昵称(需要自己的服务器)
查看>>
PHP版微信权限验证配置,音频文件下载,FFmpeg转码,上传OSS和删除转存服务器本地文件...
查看>>
教程前言 - 回归宣言
查看>>
PHP 7.1是否支持操作符重载?
查看>>
Vue.js 中v-for和v-if一起使用,来判断select中的option为选中项
查看>>
Java中AES加密解密以及签名校验
查看>>
定义内部类 继承 AsyncTask 来实现异步网络请求
查看>>
VC中怎么读取.txt文件
查看>>
如何清理mac系统垃圾
查看>>
企业中最佳虚拟机软件应用程序—Parallels Deskto
查看>>
Nginx配置文件详细说明
查看>>
怎么用Navicat Premium图标编辑器创建表
查看>>
Spring配置文件(2)配置方式
查看>>
MariaDB/Mysql 批量插入 批量更新
查看>>