booksearcherclient.java

来自「JAX-RPC 构建 RPC 服务和客户机,使用 Java API 构建基于 R」· Java 代码 · 共 63 行

JAVA
63
字号
import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class BookSearcherClient {  public static final String SERVICE_URL =    "http://localhost:8080/axis/BookSearcher.jws";  private Service service;  private Call call;  private URL serviceUrl;  public BookSearcherClient() { }  public Object[] search(String keyword) throws IOException {    try {      if (service == null) {        service = new Service();      }      if (call == null) {        call = (Call)service.createCall();      }      if (serviceUrl == null) {        serviceUrl = new URL(SERVICE_URL);      }      call.setTargetEndpointAddress(serviceUrl);           // Select operation to call      call.setOperationName(new QName("http://soapinterop.org/",                             "search"));      // Make call and get result      Object[] results = (Object[])call.invoke(new Object[] { keyword });      return results;    } catch (MalformedURLException e) {      throw new IOException("Error creating service URL at " + SERVICE_URL);    } catch (ServiceException e) {      throw new IOException("Error creating service call: " + e.getMessage());    }  }  public static void main(String[] args) throws IOException {    if (args.length != 1) {      System.err.println("Usage: java BookSearcherClient [search keyword]");      return;    }    String keyword = args[0];    BookSearcherClient client = new BookSearcherClient();    Object[] results = client.search(keyword);    System.out.println("Returned books for keyword '" + keyword + "':");    for (int i = 0; i<results.length; i++) {      System.out.println("  " + results[i]);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?