Java发布一个简单webservice应用,并发送SOAP请求

目录

一、创建并发布一个简单的webservice应用

二、客户端访问webservice

  1、通过 HttpClient 及  HttpURLConnection 发送SOAP请求,代码如下:

SOAP的请求报文的格式是怎么来的呢?


一、创建并发布一个简单的webservice应用

  1、webservice 代码:

复制代码

 1 package com.ls.demo;
 2 
 3 import javax.jws.WebMethod;
 4 import javax.jws.WebService;
 5 import javax.xml.ws.Endpoint;
 6 
 7 
 8 @WebService
 9 public class HelloWorld {
10     @WebMethod
11     public String sayHello(String str){
12         System.out.println("get Message...");
13         String result = "Hello World, "+str;
14         return result;
15     }
16     public static void main(String[] args) {
17         System.out.println("server is running");
18         String address="http://localhost:9000/HelloWorld";
19         Object implementor =new HelloWorld();
20         Endpoint.publish(address, implementor);
21     }
22 
23 }

复制代码

 

  2、运行项目,并访问 "http://localhost:9000/HelloWorld?wsdl",得到如下wsdl文件,说明webservice发布成功:

  

复制代码

 1 
 2 
 3 
 4 
 5     
 6         
 7             
 8         
 9     
10     
11         
12     
13     
14         
15     
16     
17         
18             
19             
20         
21     
22     
23         
24         
25             
26             
27             
28             
29             
30                 
31             
32         
33     
34     
35         
36             
37         
38     
39 

复制代码

 

二、客户端访问webservice

  1、通过 HttpClient 及  HttpURLConnection 发送SOAP请求,代码如下:

  

复制代码

 1 import java.io.BufferedReader;
 2 import java.io.DataOutputStream;
 3 import java.io.InputStream;
 4 import java.io.InputStreamReader;
 5 import java.net.HttpURLConnection;
 6 import java.net.URL;
 7 
 8 import org.apache.commons.httpclient.HttpClient;
 9 import org.apache.commons.httpclient.methods.PostMethod;
10 import org.apache.commons.httpclient.methods.RequestEntity;
11 import org.apache.commons.httpclient.methods.StringRequestEntity;
12 import org.apache.commons.io.IOUtils;
13 
14 public class TestHelloWrold {
15     public static void main(String[] args) throws Exception {
16         String wsdl = "http://localhost:9000/HelloWorld?wsdl";
17         int timeout = 10000;
18         StringBuffer sb = new StringBuffer("");
19         sb.append("");
20         sb.append("");
25         sb.append("");
26         sb.append("");
27         sb.append("ls");
28         sb.append("");
29         sb.append("");
30         sb.append("");
31 
32         
33         
34         // HttpClient发送SOAP请求
35         System.out.println("HttpClient 发送SOAP请求");
36         HttpClient client = new HttpClient();
37         PostMethod postMethod = new PostMethod(wsdl);
38         // 设置连接超时
39         client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
40         // 设置读取时间超时
41         client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
42         // 然后把Soap请求数据添加到PostMethod中
43         RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
44         //设置请求头部,否则可能会报 “no SOAPAction header” 的错误
45         postMethod.setRequestHeader("SOAPAction","");
46         // 设置请求体
47         postMethod.setRequestEntity(requestEntity);
48         int status = client.executeMethod(postMethod);
49         // 打印请求状态码
50         System.out.println("status:" + status);
51         // 获取响应体输入流
52         InputStream is = postMethod.getResponseBodyAsStream();
53         // 获取请求结果字符串
54         String result = IOUtils.toString(is);
55         System.out.println("result: " + result);
56 
57         
58         
59         // HttpURLConnection 发送SOAP请求
60         System.out.println("HttpURLConnection 发送SOAP请求");
61         URL url = new URL(wsdl);
62         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
63 
64         conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
65         conn.setRequestMethod("POST");
66         conn.setUseCaches(false);
67         conn.setDoInput(true);
68         conn.setDoOutput(true);
69         conn.setConnectTimeout(timeout);
70         conn.setReadTimeout(timeout);
71         
72         DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
73         dos.write(sb.toString().getBytes("utf-8"));
74         dos.flush();
75         
76         
77         BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
78         String line = null;
79         StringBuffer strBuf = new StringBuffer();
80         while ((line = reader.readLine()) != null) {
81             strBuf.append(line);
82         }
83         dos.close();
84         reader.close();
85         
86         System.out.println(strBuf.toString());
87     }
88 
89 }

复制代码

 

  

 响应报文如下:

  

复制代码


  
    
      
        Hello World, ls
      
    
  

复制代码

------------------------------------------------------------------------------------  

SOAP的请求报文的格式是怎么来的呢?

  答:(1)可用Eclipse测试WSDL文件,则可得到想要的SOAP请求及响应报文,具体步骤如下图:

 

第一步:

Java发布一个简单webservice应用,并发送SOAP请求_第1张图片

 

第二步:

 通过第一步,会在浏览器打开如下的页面

Java发布一个简单webservice应用,并发送SOAP请求_第2张图片

  

(2)saopui工具

  2、生成客户端代码访问

   a、通过 "wsimport"(JDK自带)命令生成客户端代码。进入命令行模式,执行 wsimport -s . http://localhost:9000/HelloWorld?wsdl,就会在当前目录下生成客户端代码。附图:

 Java发布一个简单webservice应用,并发送SOAP请求_第3张图片

  b、通过Eclipse生成客户端代码

 

你可能感兴趣的:(Java发布一个简单webservice应用,并发送SOAP请求)