加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService客户端——HttpClient调用WebService

发布时间:2020-12-16 21:55:56 所属栏目:安全 来源:网络整理
导读:使用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.HttpE

使用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();
	}
}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读