Java发布webservice应用并发送SOAP请求调用

webservice框架有很多,比如axis、axis2、cxf、xFire等等,做服务端和做客户端都可行,个人感觉使用这些框架的好处是减少了对于接口信息的解析,最主要的是减少了对于传递于网络中XML的解析,代价是你不得不在你的框架中添加对于这些框架的依赖。个人观点是:服务端使用这些框架还行,如果做客户端,没必要使用这些框架,只需使用httpclient即可。

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

  1、webservice 代码:

 import javax.jws.WebMethod;
 import javax.jws.WebService;
 import javax.xml.ws.Endpoint;
 
 
  @WebService
  public class HelloWorld {
     @WebMethod
     public String sayHello(String str){
         System.out.println("get Message...");
         String result = "Hello World, "+str;
         return result;
     }
     public static void main(String[] args) {
         System.out.println("server is running");
         String address="http://localhost:9000/HelloWorld";
         Object implementor =new HelloWorld();
         Endpoint.publish(address, implementor);
     }
 
 }

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

  例如天气的wsdl:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 

二、客户端访问webservice

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

  import java.io.BufferedReader;                                                                             
  import java.io.DataOutputStream;                                                                           
  import java.io.InputStream;                                                                                
  import java.io.InputStreamReader;                                                                          
  import java.net.HttpURLConnection;                                                                         
  import java.net.URL;                                                                                       
                                                                                                             
  import org.apache.commons.httpclient.HttpClient;                                                           
  import org.apache.commons.httpclient.methods.PostMethod;                                                   
  import org.apache.commons.httpclient.methods.RequestEntity;                                                
  import org.apache.commons.httpclient.methods.StringRequestEntity;                                          
  import org.apache.commons.io.IOUtils;                                                                      
                                                                                                             
  public class TestHelloWrold {                                                                              
      public static void main(String[] args) throws Exception {                                              
          String wsdl = "http://localhost:9000/HelloWorld?wsdl";                                             
          int timeout = 10000;                                                                               
          StringBuffer sb = new StringBuffer("");                                                            
          sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");                                           
          sb.append("<soap:Envelope "                                                                        
                  + "xmlns:api='http://demo.ls.com/' "                                                       
                  + "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "                                 
                  + "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "                                          
                  + "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");                              
          sb.append("<soap:Body>");                                                                          
          sb.append("<api:sayHello>");                                                                       
          sb.append("<arg0>ls</arg0>");                                                                      
          sb.append("</api:sayHello>");                                                                      
          sb.append("</soap:Body>");                                                                         
          sb.append("</soap:Envelope>");                                                                     
                                                                                                                                                                                                                       
          // HttpClient发送SOAP请求                                                                              
          System.out.println("HttpClient 发送SOAP请求");                                                         
          HttpClient client = new HttpClient();                                                              
          PostMethod postMethod = new PostMethod(wsdl);                                                      
          // 设置连接超时                                                                                          
          client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);                       
          // 设置读取时间超时                                                                                        
          client.getHttpConnectionManager().getParams().setSoTimeout(timeout);                               
          // 然后把Soap请求数据添加到PostMethod中                                                                       
          RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");         
          //设置请求头部,否则可能会报 “no SOAPAction header” 的错误                                                         
          postMethod.setRequestHeader("SOAPAction","");                                                      
          // 设置请求体                                                                                           
          postMethod.setRequestEntity(requestEntity);                                                        
          int status = client.executeMethod(postMethod);                                                     
          // 打印请求状态码                                                                                         
          System.out.println("status:" + status);                                                            
          // 获取响应体输入流                                                                                        
          InputStream is = postMethod.getResponseBodyAsStream();                                             
          // 获取请求结果字符串                                                                                       
          String result = IOUtils.toString(is);                                                              
          System.out.println("result: " + result);                                                           
                                                                                                                                                                                                                        
          // HttpURLConnection 发送SOAP请求                                                                      
          System.out.println("HttpURLConnection 发送SOAP请求");                                                  
          URL url = new URL(wsdl);                                                                           
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();                                 
                                                                                                             
          conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");                                
          conn.setRequestMethod("POST");                                                                     
          conn.setUseCaches(false);                                                                          
          conn.setDoInput(true);                                                                             
          conn.setDoOutput(true);                                                                            
          conn.setConnectTimeout(timeout);                                                                   
          conn.setReadTimeout(timeout);                                                                      
                                                                                                             
          DataOutputStream dos = new DataOutputStream(conn.getOutputStream());                               
          dos.write(sb.toString().getBytes("utf-8"));                                                        
          dos.flush();                                                                                       
                                                                                                                                                                                                                         
          BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 
          String line = null;                                                                                
          StringBuffer strBuf = new StringBuffer();                                                          
          while ((line = reader.readLine()) != null) {                                                       
              strBuf.append(line);                                                                           
          }                                                                                                  
          dos.close();                                                                                       
          reader.close();                                                                                    
                                                                                                             
          System.out.println(strBuf.toString());                                                             
      }                                                                                                                                                                                                                   
  }                                                                                                          

  响应报文如下:

<?xml version="1.0" ?>
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
      <ns2:sayHelloResponse xmlns:ns2="http://demo.ls.com/">
        <return>Hello World, ls</return>
      </ns2:sayHelloResponse>
    </S:Body>
  </S:Envelope>

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

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

   第一步:

    

  第二步:

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

  

 (2)saopui工具拿到soap报文

  soapUI是一个开源测试工具,通过soap/http来检查、调用、实现Web Service的功能/负载/符合性测试。该工具既可作为一个单独的测试软件使用,也可利用插件集成到Eclipse,maven2.X,Netbeans 和intellij中使用。soapUI pro是soapUI的商业非开源版本,实现的 功能较开源的soapUI更多。

  a、首先得安装soapUI 4.5.2,安装后打开,截图如下:

     

  b、右键点击“Projects”创建工程,截图如下:

    

  c、双击展开左侧创建的工程下所有节点,最后双击“Request 1”节点,在右侧即可拿到soap格式消息,这个就是我们后面作为客户端调用服务端的报文内容,截图如下:

    

 

 2、生成客户端代码访问

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

     

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

    

    

  (1).生成本地代码后可以直接调用,比如调用天气webservice接口: 

package hanwl.TestDemo;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import cn.com.WebXml.WeatherWebService;
import cn.com.WebXml.WeatherWebServiceLocator;
import cn.com.WebXml.WeatherWebServiceSoap;

public class TestWebservice {
    public static void main(String[] args) {
        WeatherWebService weatherWebService = new WeatherWebServiceLocator();
        WeatherWebServiceSoap  weatherWebServiceSoap = null;
        try {
            weatherWebServiceSoap = weatherWebService.getWeatherWebServiceSoap();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        String[] cityweather = null;
        //String[] city={"北京","上海","深圳","广州"};
         try {
             cityweather = weatherWebServiceSoap.getWeatherbyCityName("北京");//不输入默认为上海市
         } catch (RemoteException e) {
             e.printStackTrace(); 
        }
         
         for(String s :cityweather){
             System.out.println(s);
             System.out.println("------------------------");
         }
    }

}

  (2).httpclient作为客户端调用天气webservice

package hanwl.TestDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;

public class TestWebservice2 {

    public static void main(String[] args) throws IOException {
        
        // TODO Auto-generated method stub
        String wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";
        int timeout = 1000;
        StringBuffer sb = new StringBuffer("");          
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
              sb.append("<soapenv:Envelope "                       
                      + "    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
                      + " xmlns:q0='http://WebXml.com.cn/' "
                      + "    xmlns:xsd='http://www.w3.org/2001/XMLSchema' " 
                      + " xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' >");
              sb.append("<soapenv:Body>");
            sb.append("<q0:getWeatherbyCityName>");
            sb.append("<q0:theCityName>唐山</q0:theCityName> ");
            sb.append("</q0:getWeatherbyCityName>");
            sb.append("</soapenv:Body>");
            sb.append("</soapenv:Envelope>");
         
            // HttpClient发送SOAP请求
             System.out.println("HttpClient 发送SOAP请求");
             HttpClient client = new HttpClient();
             PostMethod postMethod = new PostMethod(wsdl);
             // 设置连接超时
             client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
             // 设置读取时间超时
             client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
             // 然后把Soap请求数据添加到PostMethod中
             RequestEntity requestEntity = new StringRequestEntity(sb.toString(), "text/xml", "UTF-8");
             //设置请求头部,否则可能会报 “no SOAPAction header” 的错误
             //postMethod.setRequestHeader("SOAPAction","");
             // 设置请求体
             postMethod.setRequestEntity(requestEntity);
             int status = client.executeMethod(postMethod);
             // 打印请求状态码
             System.out.println("status:" + status);
             // 获取响应体输入流
             InputStream is = postMethod.getResponseBodyAsStream();
             // 获取请求结果字符串
             String result = IOUtils.toString(is);
             Document dc = strXmlToDocument(result);
//             Element root = dc.getRootElement();
//             System.out.println(root.getName());
//             System.out.println("result: " + result);
        
    }
    
    public static Document strXmlToDocument(String parseStrXml){        
         Document document = null;
        try {
            document = DocumentHelper.parseText(parseStrXml);            
            Element root = document.getRootElement();
            List<Element> list = root.elements();
            getElement(list);
        } catch (DocumentException e) {
            e.printStackTrace();
        }          
        return document;
    }
    
    private static void getElement(List<Element> sonElemetList) {
//        Map<String,String> map = new HashMap<String, String>();
        for (Element sonElement : sonElemetList) {
                if (sonElement.elements().size() != 0) {
                    System.out.println(sonElement.getName() + ":");
                    getElement(sonElement.elements());
                }else{
                    System.out.println(sonElement.getName() + ":"+ sonElement.getText());
                }
 
        }
    }    
}

三、总结

       优点:

       1.使用httpclient作为客户端调用webservice,不用关注繁琐的webservice框架,只需找到SOAP消息格式,添加httpclient依赖就行。

       2.使用httpclient调用webservice,建议采用soap1.1方式调用,经测试使用soap1.1方式能调用soap1.1和soap1.2的服务端。 

       缺点:

       唯一的缺点是,你得自己解析返回的XML,找到你关注的信息内容。

参考地址:https://blog.csdn.net/zilong_zilong/article/details/53932667

     https://blog.csdn.net/gzxdale/article/details/74242359

 

转载于:https://www.cnblogs.com/loong-hon/p/10337316.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java可以通过SOAP协议调用Web Service接口。具体步骤如下: 1. 创建一个SOAP消息请求体,包括SOAP消息头和SOAP消息体。 2. 创建一个SOAP连接对象,并设置连接的URL地址。 3. 发送SOAP消息请求,等待Web Service接口返回SOAP消息响应。 4. 解析SOAP消息响应,获取需要的数据。 5. 关闭SOAP连接对象。 需要注意的是,调用Web Service接口需要提供接口的WSDL文件,以便生成客户端代码。可以使用Java自带的wsimport工具生成客户端代码,也可以使用第三方工具如Apache CXF等。 另外,调用Web Service接口时需要注意SOAP消息的格式和命名空间等问题,以确保请求和响应能够正确解析。 ### 回答2: Java是一门广泛应用于开发Web应用程序的高级编程语言。它是一门面向对象的语言,具备简单易学、可移植性强、安全性高等特点,被广泛地应用于各种软件开发领域中。Java调用WebService接口,是现在Web开发中非常重要的技术之一。 WebService(简称WS)是基于Web的远程调用协议,用于服务提供者和服务消费者之间的互操作。它是一种跨平台、跨语言的通信方式,其中SOAP(简单对象访问协议)是一种常用的WebService交互协议。Java可以通过SOAP调用WebService接口服务。 Java中使用SOAP调用WebService接口的步骤如下: 1.编写WebService客户端代码 Java提供了访问WebService接口服务的类:javax.xml.soap.SOAPConnection和javax.xml.soap.SOAPConnectionFactory。我们可以通过下面的代码获取SOAPConnection: ``` SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection(); ``` 2.创建SOAP请求SOAP请求中,我们需要设置相关的参数,包括:SOAP消息的版本、SOAP请求消息体、SOAP请求头等。下面是创建请求消息的代码示例: ``` //初始化一个SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); //创建SOAP报文头 SOAPHeader soapHeader = soapMessage.getSOAPHeader(); SOAPBody soapBody = soapMessage.getSOAPBody(); ``` 3.设置SOAP请求参数 在请求中,需要设置相关参数,包括EndPoint地址、SOAPAction、参数值等。下面是设置请求参数的代码示例: ``` //设置WebService请求地址 URL url= new URL("http://www.example.com/WebService"); //创建一个SOAP消息并设置Header和Body SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); SOAPBody soapBody = soapMessage.getSOAPBody(); //设置要调用WebService方法名 String methodName = "getWebServiceResult"; //设置SoapAction String soapAction = "http://www.example.com/WebService/getWebServiceResult"; soapMessage.getMimeHeaders().addHeader("SOAPAction", soapAction); //调用WebService SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection(); SOAPMessage response = connection.call(soapMessage, url); ``` 4.解析SOAP响应 在获取WebService接口服务的结果后,我们需要对SOAP响应进行解析。下面是解析SOAP响应的代码示例: ``` //解析响应数据 SOAPBody soapBody = response.getSOAPBody(); NodeList nodeList = soapBody.getElementsByTagName("return"); String responseStr = nodeList.item(0).getTextContent();//获取返回值 ``` 综上所述,Java调用WebService接口SOAP的过程包括了编写WebService客户端代码、创建SOAP请求、设置SOAP请求参数及解析SOAP响应等步骤。这些步骤的实现可以帮助我们更好地调用WebService接口,并获取接口的响应结果。 ### 回答3: Java 语言作为一种广泛应用于企业级应用的编程语言,自然对接webservice等网络接口是其常见的应用场景之一。在Java中,调用webservice接口有多种方法,其中最常用的是使用soap协议来调用webservice接口。 soap协议即简单对象访问协议,是一种基于XML的协议,用于将网络上的消息传递给远程执行的程序。使用soap协议调用webservice接口可以实现跨平台、跨语言、跨网络的消息传递,是一种比较常用的通信方式。 在Java中,调用webservice接口需要使用Java API for XML Web Services(JAX-WS)技术。JAX-WS是一种Java EE标准,提供了一套API,支持Java应用webservice接口之间的通信。以下是调用webservice接口的步骤: 1. 创建一个JAX-WS客户端工程(Client Project),该工程用于调用webservice接口。可以在Eclipse、IntelliJ IDEA等IDE中创建该工程。 2. 在工程中创建一个webservice客户端代理(Web Service Client Proxy),用于调用webservice接口。可以使用Eclipse这类IDE中提供的WSDL文件生成工具(例如wsimport)来生成webservice客户端代理。 3. 使用生成的webservice客户端代理来调用webservice接口。在Java中,使用webservice客户端代理对象调用webservice接口的过程类似于使用Java接口调用远程程序,可以通过SOAPMessage传输请求和响应信息,完成与webservice接口进行交互的过程。 需要注意的是,在调用webservice接口时,需要提供webservice接口的URL、命名空间(Namespace)、操作名称(Operation)等信息。另外,我们还可以为webservice接口提供请求头信息(Header)和请求参数(Parameter),以便webservice端正确地处理请求。 总之,通过Java调用webservice接口,在很多应用中都是必不可少的一步,特别是在企业级应用中更是如此。因此,我们需要深入理解soap协议和JAX-WS技术的原理和应用,才能在实际开发中进行合理的使用和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值