WebService客户端——HttpClient调用WebService

22 篇文章 0 订阅
14 篇文章 2 订阅

使用SOAP UI查看请求参数,将请求参数以xml形式进行传送。


package cxf;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 * httpclient调用webservice
 * 
 * @author muyunfei
 * 
 * <p>Modification History:</p> 
 * <p>Date       Author      Description</p>
 * <p>------------------------------------------------------------------</p>
 * <p>Jan 11, 2017           牟云飞       		 新建</p>
 */
public class Client {
public static void main(String[] args) {
	try {
		 CloseableHttpClient httpclient = HttpClients.createDefault();
		 HttpPost httpPost= new HttpPost("http://localhost:9090/****/services/CSGServiceService?wsdl");
		 //使用SOAP UI查看请求信息头部
		 String wsdlData="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.com/\">"
		   +"<soapenv:Header/>"
		   +"<soapenv:Body>"
		      +"<ser:csg_message>"
		         +"<!--Optional:-->"
		         +"<data>"+"请求参数data"+"</data>"
		         +"<!--Optional:-->"
		         +"<token>"+"请求参数token"+"</token>"
		      +"</ser:csg_message>"
		   +"</soapenv:Body>"
		+"</soapenv:Envelope>";
		 
//		 StringEntity myEntity = new StringEntity(wsdlData, 
//				   ContentType.create("application/soap+xml", "UTF-8"));
		 StringEntity myEntity = new StringEntity(wsdlData, 
				   ContentType.create("text/xml", "UTF-8"));
		 httpPost.setEntity(myEntity);
		 //发送请求
		 ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
			 public String handleResponse(
                   final HttpResponse response) throws ClientProtocolException, IOException {
               int status = response.getStatusLine().getStatusCode();
               if (status >= 200 && status < 300) {
                   HttpEntity entity = response.getEntity();
                   if(null!=entity){
                   	String result= EntityUtils.toString(entity);
              		 	return result;
                   }else{
                   	return null;
                   }
               } else {
                   throw new ClientProtocolException("Unexpected response status: " + status);
               }
           }
       };
       //返回的json对象
       String responseBody = httpclient.execute(httpPost, responseHandler);
	   httpclient.close();
       System.out.println(responseBody);
       //解析xml,获得return信息
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		StringReader sr = new StringReader(responseBody);
		InputSource is = new InputSource(sr);
		Document document = db.parse(is);
		Element root = document.getDocumentElement();
		NodeList nodelist_return = root.getElementsByTagName("return");
		String returnCont = nodelist_return.item(0).getTextContent();
		//打印返回信息
		System.out.println("webservice返回信息:"+returnCont);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牟云飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值