环境:win10 工具:Visual Studio2015 语言:vb.net
WebService:
1.打开vs2015,新建visual basic项目,选择ASP.NET Web应用程序,单击确定
2.右键解决方案,添加新建项-Web-Web 服务(ASMX)
3.生成的WebService1.asmx文件默认给了一个HelloWorld方法
4.直接运行这个WebService,等待第三方连接获取数据
备注: 生成的网页链接示例↓
http://localhost:6063/WebService1.asmx
实际第三方在调用的过程中需要加?wsdl*即↓
http://localhost:6063/WebService1.asmx?wsdl
在Soapui中调用HelloWorld方法为↓
http://localhost:6063/WebService1.asmx/HelloWorld
实际上面这一段,直接复制粘贴并不能成功获取数据
第三方程序:
需要在框架处引用System.Web.dll以及System.Web.Services.dll 这里用的是vb.net winform写
指定获取数据的地址: http://localhost:6063/WebService1.asmx
需要用到的类方法
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Net
Imports System.Web.Services.Description
Public Class WebServiceHelper
#Region "InvokeWebService"
'动态调用web服务
Public Shared Function InvokeWebService(ByVal url As String, ByVal methodname As String, ByVal args As Object()) As Object
Try
Return WebServiceHelper.InvokeWebService(url, Nothing, methodname, args)
Catch ex As Exception
Throw New Exception (ex.Message )
End Try
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 Function
End Class
调用运行测试
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim args As Object() = New Object() {""}
Dim method As String = "HelloWorld"
dim xmlstring As String = WebServiceHelper.InvokeWebService("http://localhost:6063/WebService1.asmx", method , nothing )
TextBox2.Text = xmlstring
End Sub
End Class