Axis2调用Axis1.4时出现转义

    在之前项目里面遇到其他公司开放的接口是Axis1.4版本的而我们的是Axis2的经查看资料是可以调用的,于是我就用项目公用的调用方法,调用后报500。于是我又百度了一下调用方法,一直都不行我后面把报文打印了出来,一看就发现被服务端转义了。去度娘查了下看见很多人都遇到了这个问题有些人是通过修改服务端代码解决了,也想了加1.4的jar包不过1.4的要引入4个怕跟项目里面的冲突而且加的话需要几轮测试比较耗资源,可是我这边是人家项目跟那边沟通了一下人家不愿意修改于是想到去看Axis的源码,我看了一下原来还是用Soket去调的,那就好办了于是就写了一个工具类,代码如下:

package com.cliffter.webservice.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringEscapeUtils;

public class WebServiceClientUtil {
	private  Map<String,String> requestProperty;	//请求头内容
	
	private String requestMethod = "POST";		//请求方式   --> 默认POST
		
	private WebServiceClientUtil webServiceClientUtil;
	
	private WebServiceClientUtil(){}
	
	private static WebServiceClientUtil initWebServiceClientUtil(){
		WebServiceClientUtil webServiceClientUtil = new WebServiceClientUtil();
		webServiceClientUtil.defultRequestProperty();
		return webServiceClientUtil;
	}
	/**
	 * 无返回的请求
	 * @param host	请求地址
	 * @param requestProperty	请求头内容
	 * @param xmlParam	请求内容 (xml格式)
	 * @param requestMethod 请求方式
	 */
	public static void invokeToVoid(String host,Map<String,String> requestProperty,String xmlParam,String requestMethod){
		WebServiceClientUtil webServiceClientUtil = initWebServiceClientUtil();
		webServiceClientUtil.getInput(host, requestProperty, xmlParam, requestMethod, false);
	}
	
	/**
	 * @param host	请求地址
	 * @param requestProperty	请求头内容
	 * @param xmlParam	请求内容 (xml格式)
	 * @param requestMethod 请求方式
	 * @return 返XML格式字符串
	 */
	public static String invokeToXmlData(String host,Map<String,String> requestProperty,String xmlParam,String requestMethod){
		WebServiceClientUtil webServiceClientUtil = initWebServiceClientUtil();
		return webServiceClientUtil.getInput(host, requestProperty, xmlParam, requestMethod, true);
	}
	
	/**
	 * 
	 * @param host	请求地址
	 * @param xmlParam	请求内容 (xml格式)
	 * @param requestMethod 请求方式
	 * @return 返XML格式字符串
	 */
	public static String invokeToXmlData(String host,String xmlParam,String requestMethod){
		WebServiceClientUtil webServiceClientUtil = initWebServiceClientUtil();
		return webServiceClientUtil.getInput(host, webServiceClientUtil.requestProperty, xmlParam, requestMethod, true);

	}
	
	
	/**
	 * 
	 * @param host
	 * @param xmlParam
	 * @return 返XML格式字符串
	 */
	public static String invokeToXmlData(String host,String xmlParam){
		WebServiceClientUtil webServiceClientUtil = initWebServiceClientUtil();
		return webServiceClientUtil.getInput(host, webServiceClientUtil.requestProperty, xmlParam, webServiceClientUtil.requestMethod, true);
	}
	
	
	//请求操作
	private static String getInput(String host,Map<String,String> requestProperty,String xmlParam,String requestMethod,boolean isInput){
		HttpURLConnection urlConnection = null;
		DataOutputStream out = null;
		String result = "";
		try {
			//创建连接对象
			URL url = new URL(host);
			urlConnection = (HttpURLConnection)url.openConnection();
			//填写请求头部信息
			
			urlConnection.setRequestMethod(requestMethod);	//为post请求
			urlConnection.setDoOutput(true);		//有请求数据
			if(isInput){
				urlConnection.setDoInput(true);
			}
			for (String key : requestProperty.keySet()) {
				urlConnection.setRequestProperty(key, requestProperty.get(key));
			}
			
			out = new DataOutputStream(urlConnection.getOutputStream());
			out.writeBytes(xmlParam); 

			out.flush();
			out.close();
			
			if(isInput){
				BufferedReader in = null;
		        try {
		        	in = new BufferedReader(
		        			new InputStreamReader(
		        					urlConnection.getInputStream()));
		        	String line;
					while ((line = in.readLine()) != null) {
					    result += StringEscapeUtils.unescapeHtml(line);
					}
					
				} catch (IOException e) {
					System.err.println(WebServiceClientUtil.class+":读取数据时错误");
					e.printStackTrace();
				}finally{
					in.close();
				}
			}	
		} catch (MalformedURLException e) {
			System.err.println(WebServiceClientUtil.class +":创建URL时错误");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println(WebServiceClientUtil.class +":创建HttpURLConnection时错误");
			e.printStackTrace();
		}
		
		return result;
	}
	
	//默认的请求头
	private  void defultRequestProperty(){
		this.requestProperty = new HashMap();
		this.requestProperty.put("connection", "Keep-Alive");
		this.requestProperty.put("Content-Type", "text/xml;charset=UTF-8");
		this.requestProperty.put("Accept-Language", "zh-cn");
		this.requestProperty.put("SOAPAction", "");

	}
	
}

 

转载请注明原文链接:Axis2调用Axis1.4时出现转义

发表评论:

共有 0 条评论

 Top