起步软件技术论坛-X3

 找回密码
 立即注册
搜索
查看: 679|回复: 6

[分享]Java调用WebService**

[复制链接]
发表于 2008-1-10 11:26:05 | 显示全部楼层 |阅读模式
1 到 http://apache.mirror.phpchina.com/ws/axis/1_4/ 下载 axis 包
  解压到本地目录 c:\test ,在这个目录下会产生
  axis.jar
  axis-ant.jar
  commons-discovery-0.2.jar
  commons-logging-1.0.4.jar
  jaxrpc.jar
  log4j-1.2.8.jar
  wsdl4j-1.5.1.jar
  saaj.jar

2 到 http://java.sun.com/products/javamail/downloads/index.html 下载mail.jar 到 c:\test目录
  到http://java.sun.com/products/javabeans/jaf/downloads/index.html 下载 activation.jar  到 c:\test 目录

3 在dos命令行执行
  c:\test>java -classpath ".;commons-logging-1.0.4.jar;axis.jar;commons-discovery-0.2.jar;jaxrpc.jar;wsdl4j-1.5.1.jar;saaj.jar;activation.jar;mail.jar" org.apache.axis.wsdl.WSDL2Java -o "myws" http://10.10.10.111/WebService/ManageUsersService.asmx?wsdl
就会在c:\test\myws目录下生成WebService调用需要的Java类

4 在Java代码中调用这些类就可以了
  public static void main(String[] args) {
    ManageUsersService abc = new ManageUsersServiceLocator( );
    URL url = new URL( "http://10.10.10.111/WebService/ManageUsersService.asmx" );
    ManageUsersServicePortType pt = abc.getManageUsersServiceHttpPort( url );
    //...
  }
回复

使用道具 举报

 楼主| 发表于 2008-1-10 13:26:49 | 显示全部楼层
这样,X3平台上调用WebService除了《导入第三方控件》文档中说的,在Delphi中封装一下,在客户端调用之外
还可以在服务端调用,客户端通过调用 远程Java 的方式直接得到服务端调用的返回结果
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-16 16:27:25 | 显示全部楼层
干脆把导入第三方控件方式的文档也贴到这里,看着方便
============================
第五章 如何在X3平台调用WebService

在平台上调用WebService是通过把WebService注册成一个接口,通过调用接口的方法来调用WebService。
1.        生成接口文件
执行 Delphi6Binwsdlimp.exe为WebService生成接口文件
命令行参数说明

1.png

115.11 KB, 下载次数: 371

回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-16 16:27:59 | 显示全部楼层
2.        利用导入第三方控件工具把生成的接口文件.pas导入到平台中
因为平台上不支持缺省参数,在生成的接口文件中,把
function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''): ServiceSoap;
修改为
function GetServiceSoap(UseWSDL: Boolean; Addr: string): ServiceSoap;

3.        在平台上调用GetServiceSoap函数来创建接口,然后就可以调用接口的方法来调用WebService

说明:
X3平台的2323版本开始支持接口,之前的版本需要在导入之前封装成对象来使用,具体把接口封装成对象的说明请参考《如何在X3平台导入外部控件》
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-16 16:28:14 | 显示全部楼层
举例:
WebService文件
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
     Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

End Class
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-16 16:28:33 | 显示全部楼层
生成的接口文件
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL     : http://localhost:3518/WebSite1/Service.asmx?WSDL
// Encoding : utf-8
// Version  : 1.0
// (2006-8-2 9:38:19 - $Revision:   1.9.1.0.1.0.1.9  $)
// ************************************************************************ //

unit MyService;

interface

uses InvokeRegistry, Types, XSBuiltIns;

type

  // ************************************************************************ //
  // The following types, referred to in the WSDL document are not being represented
  // in this file. They are either aliases[@] of other types represented or were referred
  // to but never[!] declared in the document. The types from the latter category
  // typically map to predefined/known XML or Borland types; however, they could also
  // indicate incorrect WSDL documents that failed to declare or import a schema type.
  // ************************************************************************ //
  // !:string          - "http://www.w3.org/2001/XMLSchema"


  // ************************************************************************ //
  // Namespace : http://tempuri.org/
  // soapAction: http://tempuri.org/HelloWorld
  // transport : http://schemas.xmlsoap.org/soap/http
  // binding   : ServiceSoap
  // service   : Service
  // port      : ServiceSoap
  // URL       : http://localhost:3518/WebSite1/Service.asmx
  // ************************************************************************ //
  ServiceSoap = interface(IInvokable)
  ['{77573149-9C57-FA51-F11F-EFD527C91BD9}']
    function  HelloWorld: String; stdcall;
  end;

function GetServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''): ServiceSoap;


implementation
  uses SOAPHTTPClient;

function GetServiceSoap(UseWSDL: Boolean; Addr: string): ServiceSoap;
const
  defWSDL = 'http://localhost:3518/WebSite1/Service.asmx?WSDL';
  defURL  = 'http://localhost:3518/WebSite1/Service.asmx';
  defSvc  = 'Service';
  defPrt  = 'ServiceSoap';
var
  RIO: THTTPRIO;
begin
  Result := nil;
  if (Addr = '') then
  begin
    if UseWSDL then
      Addr := defWSDL
    else
      Addr := defURL;
  end;
  RIO := THTTPRIO.Create(nil);
  try
     if UseWSDL then
    begin
      RIO.WSDLLocation := Addr;
      RIO.Service := defSvc;
      RIO.Port := defPrt;
    end else
      RIO.URL := Addr;
    Result := (RIO as ServiceSoap);
  finally
    if Result = nil then
      RIO.Free;
  end;
end;


initialization
  InvRegistry.RegisterInterface(TypeInfo(ServiceSoap), 'http://tempuri.org/', 'utf-8');
  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ServiceSoap), 'http://tempuri.org/HelloWorld');

end.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-1-16 16:28:47 | 显示全部楼层
平台上调用方法:
var
  s: ServiceSoap;
begin
{  s := JustepExt.MyService.GetServiceSoap(False, 'http://localhost:3518/WebSite1/Service.asmx');
  Dialogs.ShowMessage(s.HelloWorld);
  s := nil;
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Justep Inc.

GMT+8, 2025-7-5 21:11 , Processed in 0.044235 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表