Java 30

10.编写Client代码

创建一个类:SayHelloClient

image

package org.gnuhpc.wsClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.xml.ws.BindingProvider;

public class SayHelloClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SayHelloService shs = new SayHelloService();
        SayHello sh = (SayHello) shs.getSayHelloPort();
        ((BindingProvider) sh).getRequestContext().put(
                BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
                "
http://localhost:8080/wsServerExample");
        System.out.println(((BindingProvider) sh).toString());

        String userName = null;
        boolean exit = false;
        while (!exit) {
            System.out.print("/n Please enter yourname (type 'quit' to exit): ");
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            try {
                userName = br.readLine();
            } catch (IOException e) {
                System.out.println("Errorreadingname.");
                System.exit(1);
            }
            if (!(exit = userName.trim().equalsIgnoreCase("quit")
                    || userName.trim().equalsIgnoreCase("exit"))) {
                System.out.println(sh.getGreeting(userName));
            }
        }
        System.out.println("/nThank you for running the client.");
    }
}

当你运行SayHelloClient时,它创建了一个新的Service--SayHelloService,这是通过Ant脚本调用wsimport产生的一个proxy,用来调用目标服务端点的操作。然后Client得到请求上下文,添加端点地址http://localhost:8080/wsServerExample ,在这里处理请求消息。

11.运行Client

右键SayHelloClient.java,选择Run As> Java Application,得到:

image

可以使用脚本完成对Server和Client的调用:

在Client中建立文件buildall.xml:

posted @ 2022-01-28 20:54  华茹  阅读(27)  评论(0)    收藏  举报