各种调用WebService的方法

摘自:Web服务系列教学-如何调用WebService

4.1 使用PowerBuilder调用

  适用版本8.0 需下载Bulletin Web Services Toolkit 4.1

  4.2使用java调用

需要下载apache soap。下载地址:http://xml.apache.org/soap/index.html
导入:
import org.apache.soap.*;
import org.apache.soap.rpc.*;

例程:

import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;

public class testClient {

    public static void main(String[] args) throws Exception {

        URL url = new URL ("http://192.168.0.4/yundan/service1.wsdl");
        //改成你的地址
        SOAPMappingRegistry smr = new SOAPMappingRegistry ();
        StringDeserializer sd = new StringDeserializer ();
        smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName ("", "Result"), null, null, sd);

        // 创建传输路径和参数
        SOAPHTTPConnection st = new SOAPHTTPConnection();

        // 创建调用
        Call call = new Call ();
        call.setSOAPTransport(st);
        call.setSOAPMappingRegistry (smr);

        call.setTargetObjectURI ("http://tempuri.org/message/");
        call.setMethodName("addNumbers");
        call.setEncodingStyleURI ("http://schemas.xmlsoap.org/soap/encoding/");

        Vector params = new Vector();
        params.addElement(new Parameter("NumberOne", Double.class, "10", null));
        params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
        call.setParams(params);

        Response resp = null;

        try {
          resp = call.invoke (url, "http://tempuri.org/action/Hello2.addNumbers");
        }
        catch (SOAPException e) {
        System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " + e.getMessage ());
        return;
        }

        // 检查返回值
        if (resp != null && !resp.generatedFault()) {
        Parameter ret = resp.getReturnValue();
        Object value = ret.getValue();

        System.out.println ("Answer--> " + value);
        }
        else {
            Fault fault = resp.getFault ();
            System.err.println ("Generated fault: ");
            System.out.println (" Fault Code = " + fault.getFaultCode());
            System.out.println (" Fault String = " + fault.getFaultString());
        }
    }
}

4. 3 在asp中使用Jscript调用

需下载msSoapToolkit20.exe
引用:MSSOAP.SoapClient

例程:

<%@ LANGUAGE = JScript %>
<HTML>
<HEAD>
<TITLE>webservice演示</TITLE>
</HEAD>
<BODY>
    <%
         var WSDL_URL = "http://server0/yundan/webservice1.asmx?WSDL"
           var a, b, res
           var soapclient
            a = 12
            b = 13   
        soapclient = Server.CreateObject("MSSOAP.SoapClient")
        soapclient.ClientProperty("ServerHTTPRequest") = true
‘在ASP中运行 需要设置ServerHTTPRequest选项
soapclient.mssoapinit("http://192.168.0.4/yundan/Service1.WSDL","Service1","Service1Soap","")
        res = soapclient.test(2,3)
%>
<h3>webservice 演示</h3>
<B>Result:</B> <%=res%><P><P>
</BODY>
</HTML>

4. 4在asp中使用vbscript调用

需下载msSoapToolkit20.exe
引用:MSSOAP.SoapClient

例程:

<%@ LANGUAGE = VBScript %>
<HTML>
<HEAD>
<TITLE>webservie演示</TITLE>
</HEAD>
<BODY>
<%
Dim soapclient
Const WSDL_URL = "http://192.168.0.4/yundan/service1.wsdl"
    set soapclient = Server.CreateObject("MSSOAP.SoapClient")
    soapclient.ClientProperty("ServerHTTPRequest") = True
    soapclient.mssoapinit
http://192.168.0.4:8080/yundan/Service1.WSDL","Service1","Service1Soap",""
Dim res
        res = soapclient.test(23, 34)
%>
<h1>webservie演示</h1>
<B>Result:</B> <%=res%><P><P>
</BODY>
</HTML>

 

4. 5使用C#调用

无需下载任何组件
Visual项目新建 windows应用程序。C#项目

在解决方案资源管理器中添加web引用,输入wsdl文件所在地址。

将web引用改名.
yundan.(service_name)即可引用
*需引用System.web.services*

例程:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace csharp
{
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox textBox1;
            private System.ComponentModel.Container components = null;
            public Form1()
            {
                InitializeComponent();
            }
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
            #region Windows Form Designer generated code
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.SuspendLayout();
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(88, 48);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(91, 14);
                this.label1.TabIndex = 0;
                this.label1.Text = "Webservice演示";
                this.textBox1.Location = new System.Drawing.Point(88, 128);
                this.textBox1.Name = "textBox1";
                this.textBox1.TabIndex = 1;
                this.textBox1.Text = "textBox1";
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.AddRange(new System.Windows.Forms.Control[]
{
                this.textBox1,
                this.label1
});
                this.Name = "Form1";
                this.Text = "C#Webservie演示";
                this.Load += new System.EventHandler(this.Form1_Load);
                this.ResumeLayout(false);
            }
            #endregion
            [STAThread]
            static void Main()
            {
                Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                int str;
                你的web引用的名字.Service1 cc=new 你的web引用的名字.Service1();
                str=cc.test(123,324);
                textBox1.Text=str.ToString();
            }
        }
}

4.6使用javascript调用

需下载msSoapToolkit20.exe
引用:MSSOAP.SoapClient

例程:
var WSDL_URL = "http://192.168.0.4/yundan/service1.wsdl"
WScript.echo("Connecting: " + WSDL_URL)
var Calc = WScript.CreateObject("MSSOAP.SoapClient")
Calc.mssoapinit(WSDL_URL, "", "", "")
var Answer
Answer = Calc.test(14,28)
WScript.Echo("14+28=" + Answer)
将其存成domo.js文件,直接双击运行。

4.7使用vb.net调用

无需下载任何组件
Visual项目新建 windows应用程序。Basic项目
在解决方案资源管理器中添加web引用,输入wsdl文件所在地址。
将web引用改名为yundan.
yundan.(service_name)即可引用
*需引用System.web.services*

例程:

Public Class Form1
        Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ComponentModel.IContainer
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(96, 40)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(91, 14)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Webservice演示"
        Me.TextBox1.Location = New System.Drawing.Point(88, 144)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "TextBox1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.Label1})
        Me.Name = "Form1"
        Me.Text = "VB.net webserive演示"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim cc As yundan.Service1 = New yundan.Service1()
        TextBox1.Text = cc.test(12, 123)
    End Sub
End Class

4.8使用vb6.0调用

需下载msSoapToolkit20.exe
添加引用:Microsoft Soap Type Library
位置:”C:\Program Files\Common Files\MSSoap\Binaries\ MSSOAP1.dll”
    调用方法:
    Dim cc As New MSSOAPLib.SoapClient

例程:
添加一个button控件Command1添加3个textbox控件,text1,text2,text3标准EXE

    代码如下:
Option Explicit
Dim cc As New MSSOAPLib.SoapClient
Private Sub Command1_Click()
cc.mssoapinit "http://192.168.0.4/yundan/Service1.asmx?WSDL"
Me.Text3.Text = cc.test(CInt(Text1.Text), CInt(Text2.Text))
End Sub

4.9使用vbscript调用

需下载msSoapToolkit20.exe
引用:MSSOAP.SoapClient

例程:
Option Explicit
Const WSDL_URL = "http://192.168.0.4/yundan/service1.wsdl"
WScript.echo "Connecting: " & WSDL_URL
Dim Calc
Set Calc = CreateObject("MSSOAP.SoapClient")
Calc.mssoapinit WSDL_URL
Dim Answer
Answer = Calc.test(14,28)
WScript.Echo "14+28=" & Answer
将其存成domo.vbs文件,直接双击运行。

4.10使用vc调用


posted @ 2006-02-21 20:03  杰客  阅读(5736)  评论(0)    收藏  举报