基于Httpclient的Webservicer

用SOAPUI 生成报文! 然后 用httpclient 代替cxf等webservice 框架 实现webservice 接口调用!!

154239_XPZc_3061208.png 

package com.sinosoft.test;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class TestPro {
	
	public static void main(String[] args) {
		String url = "";
		String param = "";
		test(param, url);
	}
	
	public static void test(String param, String url){
		
		System.out.println("请求报文:" + param);
		
		try {
			String result = doPost2webserivce(url, buiderPostParam(param), "");
			System.out.println(result);
			
            if(result != null) {
                result = getResponseBody(result);
                System.out.println("请求处理报文:" + result);
                
            } else {
            	System.out.println("没有收到响应数据");
            }
		 	
		} catch (Exception e) {
			e.printStackTrace();
		};
	}
	
    public static String buiderPostParam(String param) {
        StringBuilder paramBuilder = new StringBuilder();
        paramBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.wspre.sinosoft.com\">");
        paramBuilder.append("<soapenv:Header/>");
        paramBuilder.append("<soapenv:Body>");
        paramBuilder.append("<web:service>");
        paramBuilder.append("<![CDATA[");
        paramBuilder.append(param);
        paramBuilder.append("]]>");
        paramBuilder.append("</web:service>");
        paramBuilder.append("</soapenv:Body>");
        paramBuilder.append("</soapenv:Envelope>");
        return paramBuilder.toString();
    }
    
    private static String doPost2webserivce(String webServiceUrl, String param, String soapAction) throws Exception {
        String result = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            //设置请求超时
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(600000).setConnectTimeout(600000)
                    .setSocketTimeout(600000).build();

            HttpPost httpost = new HttpPost(webServiceUrl);
            httpost.setConfig(requestConfig);
            httpost.setHeader("Content-Type", "text/xml;charset=GBK");
            httpost.setHeader("SOAPAction", soapAction);

            StringEntity stringEntity = new StringEntity(param,"GBK");
            httpost.setEntity(stringEntity);

            CloseableHttpResponse response = httpclient.execute(httpost);
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity);
                }
            }
            response.close();
        } finally {
            httpclient.close();
        }
        return result;
    }
    
    private static String getResponseBody(String result) throws Exception {
        Document document = DocumentHelper.parseText(result);
        Element rootElement = document.getRootElement();
        Element returnElment = rootElement.element("Body").element("responseInfo");
        return returnElment.asXML();
    }
}

 

转载于:https://my.oschina.net/dreamVip/blog/1519338

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值