vb.net调用webService源码

页面导航:首页 > 软件编程 > vb.net > vb.net调用webService源码

vb.net调用webService源码

来源: 作者: 时间:2016-01-15 15:10 【

[csharp] Imports System.CodeDomImports System.CodeDom.CompilerImports System.Security.PermissionsImports System.Web.Services.DescriptionImports System.ReflectionPublic Class DynamicWebServ...

[csharp]  Imports System.CodeDom 
Imports System.CodeDom.Compiler 
Imports System.Security.Permissions 
Imports System.Web.Services.Description 
Imports System.Reflection 
 
Public Class DynamicWebService 
    Public Function CallWebService(ByVal webServiceAsmxUrl As String, _ 
       ByVal serviceName As String, ByVal methodName As String, _ 
       ByVal args() As Object) As Object 
 
        Try 
            Dim client As System.Net.WebClient = New System.Net.WebClient() 
 
            '-Connect To the web service 
            Dim stream As System.IO.Stream = _ 
                client.OpenRead(webServiceAsmxUrl + "?wsdl") 
 
            'Read the WSDL file describing a service. 
            Dim description As ServiceDescription = ServiceDescription.Read(stream) 
 
            'LOAD THE DOM''''''''''''''''''''''''''' 
 
            '--Initialize a service description importer. 
            Dim importer As ServiceDescriptionImporter = New ServiceDescriptionImporter() 
            importer.ProtocolName = "Soap12" ' Use SOAP 1.2. 
            importer.AddServiceDescription(description, Nothing, Nothing) 
 
            '--Generate a proxy client.  
 
            importer.Style = ServiceDescriptionImportStyle.Client 
            '--Generate properties to represent primitive values. 
            importer.CodeGenerationOptions = _ 
                 System.Xml.Serialization.CodeGenerationOptions.GenerateProperties 
 
            'Initialize a Code-DOM tree into which we will import the service. 
            Dim nmspace As CodeNamespace = New CodeNamespace() 
            Dim unit1 As CodeCompileUnit = New CodeCompileUnit() 
            unit1.Namespaces.Add(nmspace) 
 
            'Import the service into the Code-DOM tree.  
            'This creates proxy code that uses the service. 
 
            Dim warning As ServiceDescriptionImportWarnings = _ 
                           importer.Import(nmspace, unit1) 
 
            If warning = 0 Then 
 
                '--Generate the proxy code 
                Dim provider1 As CodeDomProvider = _ 
                          CodeDomProvider.CreateProvider("VB") 
                '--Compile the assembly proxy with the // appropriate references  
                Dim assemblyReferences() As String 
                assemblyReferences = New String() {"System.dll", _ 
                    "System.Web.Services.dll", "System.Web.dll", _ 
                    "System.Xml.dll", "System.Data.dll"} 
                Dim parms As CompilerParameters = New CompilerParameters(assemblyReferences) 
                parms.GenerateInMemory = True '(Thanks for this line nikolas) 
                Dim results As CompilerResults = provider1.CompileAssemblyFromDom(parms, unit1) 
 
                '-Check For Errors 
                If results.Errors.Count > 0 Then 
 
                    Dim oops As CompilerError 
                    For Each oops In results.Errors 
                        System.Diagnostics.Debug.WriteLine("========Compiler error============") 
                        System.Diagnostics.Debug.WriteLine(oops.ErrorText) 
                    Next 
                    Throw New System.Exception("Compile Error Occurred calling webservice.") 
                End If 
 
                '--Finally, Invoke the web service method 
                Dim wsvcClass As Object = results.CompiledAssembly.CreateInstance(serviceName) 
                Dim mi As MethodInfo = wsvcClass.GetType().GetMethod(methodName) 
                Return mi.Invoke(wsvcClass, args) 
 
            Else 
                Return Nothing 
            End If 
 
        Catch ex As Exception 
            Throw ex 
        End Try 
    End Function 
End Class 

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Security.Permissions
Imports System.Web.Services.Description
Imports System.Reflection

Public Class DynamicWebService
    Public Function CallWebService(ByVal webServiceAsmxUrl As String, _
       ByVal serviceName As String, ByVal methodName As String, _
       ByVal args() As Object) As Object

        Try
            Dim client As System.Net.WebClient = New System.Net.WebClient()

            '-Connect To the web service
            Dim stream As System.IO.Stream = _
                client.OpenRead(webServiceAsmxUrl + "?wsdl")

            'Read the WSDL file describing a service.
            Dim description As ServiceDescription = ServiceDescription.Read(stream)

            'LOAD THE DOM'''''''''''''''''''''''''''

            '--Initialize a service description importer.
            Dim importer As ServiceDescriptionImporter = New ServiceDescriptionImporter()
            importer.ProtocolName = "Soap12" ' Use SOAP 1.2.
            importer.AddServiceDescription(description, Nothing, Nothing)

            '--Generate a proxy client.

            importer.Style = ServiceDescriptionImportStyle.Client
            '--Generate properties to represent primitive values.
            importer.CodeGenerationOptions = _
                 System.Xml.Serialization.CodeGenerationOptions.GenerateProperties

            'Initialize a Code-DOM tree into which we will import the service.
            Dim nmspace As CodeNamespace = New CodeNamespace()
            Dim unit1 As CodeCompileUnit = New CodeCompileUnit()
            unit1.Namespaces.Add(nmspace)

            'Import the service into the Code-DOM tree.
            'This creates proxy code that uses the service.

            Dim warning As ServiceDescriptionImportWarnings = _
                           importer.Import(nmspace, unit1)

            If warning = 0 Then

                '--Generate the proxy code
                Dim provider1 As CodeDomProvider = _
                          CodeDomProvider.CreateProvider("VB")
                '--Compile the assembly proxy with the // appropriate references
                Dim assemblyReferences() As String
                assemblyReferences = New String() {"System.dll", _
                    "System.Web.Services.dll", "System.Web.dll", _
                    "System.Xml.dll", "System.Data.dll"}
                Dim parms As CompilerParameters = New CompilerParameters(assemblyReferences)
                parms.GenerateInMemory = True '(Thanks for this line nikolas)
                Dim results As CompilerResults = provider1.CompileAssemblyFromDom(parms, unit1)

                '-Check For Errors
                If results.Errors.Count > 0 Then

                    Dim oops As CompilerError
                    For Each oops In results.Errors
                        System.Diagnostics.Debug.WriteLine("========Compiler error============")
                        System.Diagnostics.Debug.WriteLine(oops.ErrorText)
                    Next
                    Throw New System.Exception("Compile Error Occurred calling webservice.")
                End If

                '--Finally, Invoke the web service method
                Dim wsvcClass As Object = results.CompiledAssembly.CreateInstance(serviceName)
                Dim mi As MethodInfo = wsvcClass.GetType().GetMethod(methodName)
                Return mi.Invoke(wsvcClass, args)

            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class


调用:


[csharp]  Imports System.Net 
Public Class Form1    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim WebserviceUrl As String = "http://192.168.100.100:9091/esbbusservice.asmx" 
 
        'specify service name 
        Dim serviceName As String = "EsbBusService" 
 
        'specify method name to be called 
        Dim methodName As String = "Process" 
 
        'Arguments passed to the method 
 
 
        Dim objCallWS As New DynamicWebService 
        Dim sSessionID = objCallWS.CallWebService(WebserviceUrl, serviceName, methodName, {txtInput.Text}) 
        TextBox1.Text = sSessionID 
 
    End Sub 
End Class 

Imports System.Net
Public Class Form1  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim WebserviceUrl As String = ""

        'specify service name
        Dim serviceName As String = "EsbBusService"

        'specify method name to be called
        Dim methodName As String = "Process"

        'Arguments passed to the method


        Dim objCallWS As New DynamicWebService
        Dim sSessionID = objCallWS.CallWebService(WebserviceUrl, serviceName, methodName, {txtInput.Text})
        TextBox1.Text = sSessionID

    End Sub
End Class


 

Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<