.Net,ASP,VB调用webservice的示例

本文介绍了一个简单的示例,展示了如何通过.NET、ASP和VB来调用WebService。其中包括了服务端的实现代码以及客户端的调用示例,覆盖了ASP.NET、VB6和ASP的不同调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.service文件代码:
Imports System.Web.Services

<System.Web.Services.WebService(Namespace := "http://tempuri.org/VSService/Service1")> _
Public Class Service1
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call

    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    ' WEB SERVICE EXAMPLE
    ' The HelloWorld() example service returns the string Hello World.
    ' To build, uncomment the following lines then save and build the project.
    ' To test this web service, ensure that the .asmx file is the start page
    ' and press F5.
    '
    '<WebMethod()> _
    'Public Function HelloWorld() As String
    '   Return "Hello World"
  'End Function

  <WebMethod()> Public Function add(ByVal a As Integer, ByVal b As Integer) As String
    add = CStr(a + b)
  End Function
End Class

2. asp.net调用,添加Web References。
 Dim s As String
 Dim t As New mingservice.Service1
 ResultAdd.Text = t.add(CInt(Param1.Text), CInt(Param2.Text))

3.  VB6调用,安装SoapToolkit20.exe。添加References:C:/Program Files/Common Files/MSSoap/Binaries/MSSOAP1.dll.
Dim sc As New MSSOAPLib.SoapClient
sc.mssoapinit "
http://192.168.0.8/VSService/Service1.asmx?WSDL"
Text3.Text = sc.Add(CInt(Text1.Text), CInt(Text2.Text))

4.ASP调用,安装SoapToolkit20.exe。添加References:C:/Program Files/Common Files/MSSoap/Binaries/MSSOAP1.dll.
<%
Dim t1,t2,t3,SSO
t1 = Request.Form("text1")
t2 = Request.Form("text2")
SET  SSO = Server.CreateObject("MSSOAP.SoapClient") 
SSO.ClientProperty("ServerHTTPRequest")  =  True 
Call  SSO.mssoapinit("
http://192.168.0.8/VSService/Service1.asmx?WSDL") 
t3=SSO.add(CInt(t1),CInt(t2)) 
%>
<form action="http.asp" METHOD="POST">
  <div align="center" >
  <input type="text" name="text1" value="0">
  <input type="text" name="text2" value="0">
  <input type="text" name="text3" value="<%=t3%>">
  </div>
<br>
 <div align="center" ><input type="submit" name="Submit" value="Ìá½»"></div>
</form>

5. ASP HTTP请求:
<%
Dim t1,t2,t3
t1 = Request.Form("text1")
t2 = Request.Form("text2")
url = "
http://192.168.0.8/VSService/Service1.asmx/add"
SoapRequest="a="&t1&"&b="&t2&""
Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP")
xmlhttp.Open "POST",url,false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "HOST","192.168.0.8"
xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest)
xmlhttp.Send(SoapRequest)

If xmlhttp.Status = 200 Then
 Set xmlDOC = server.CreateObject("MSXML.DOMDocument")
 xmlDOC.load(xmlhttp.responseXML)
 t3=xmlDOC.childNodes(1).Text
 Set xmlDOC = nothing
Else
 Response.Write xmlhttp.Status&"&nbsp;"
 Response.Write xmlhttp.StatusText
End if
Set xmlhttp = Nothing
%>
<form action="http.asp" METHOD="POST">
  <div align="center" >
  <input type="text" name="text1" value="0">
  <input type="text" name="text2" value="0">
  <input type="text" name="text3" value="<%=t3%>">
  </div>
<br>
 <div align="center" ><input type="submit" name="Submit" value="Ìá½»"></div>
</form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值