htppClient调用webservice,并解析返回的xml字符串

package com.amc.sggk;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpClientDemo {

	/**
	 * 接口调用
	 * 
	 * @param url 接口地址 json 为参数 参考(gson.getInstance.toJson(new
	 *            HashMap("参数名","参数值")))需要转换  * 成json串格式的
	 */
	public static String doClient(String url, String json) {
		return doPost(url, json);

	}

	/**
	 * 获取连接
	 * 
	 * @return CloseableHttpClient对象
	 */
	public static CloseableHttpClient buildHttpClient() {
		CloseableHttpClient client = null;
		HttpClientBuilder build = HttpClients.custom();
		client = build.build();
		return client;
	}

	/**
	 * 进行Post请求
	 * 
	 * @param url  服务地址
	 * @param json 添加json串
	 * @return String结果
	 */
	public static String doPost(String url, String json) {
		CloseableHttpClient httpClient = buildHttpClient();
		HttpPost httpPost = new HttpPost(url);
		String result = null;
		try {
			httpPost.setHeader("content-type", "application/xml;charset=UTF-8");//
			StringEntity se = new StringEntity(json, "utf-8");
			httpPost.setEntity(se);
			HttpResponse response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				result = EntityUtils.toString(entity, "UTF-8");
			}
		} catch (ClientProtocolException e) {
			// logger.error("进行模拟HTTP请求时候发生异常 ,请求地址"+url,e);
		} catch (UnsupportedEncodingException e) {
			// logger.error("进行模拟HTTP请求时候发生异常,请求地址"+url,e);
		} catch (Exception e) {
			// logger.error("进行模拟HTTP请求时候发生异常,请求地址"+url,e);
		} finally {
			// 关闭连接,释放资源
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		String url = "http://192.168.142.101:83/WebServiceForAndroid.asmx/FaultInfoTest";
		String doPost = doPost(url,"");
		System.out.println(doPost);
	}
	
	
}

 

 

解析返回的xml字符串

 

package com.amc.sggk;


import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

 
public class HttpClientTest {
 
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		String xml = " single_trade_query utf-8 2088001513232645 20090422577264 ";
		Document document = DocumentHelper.parseText(xml);
		Element root = document.getRootElement();
		List elements = root.elements();
		for (Iterator it = elements.iterator(); it.hasNext();) {
			Element element = it.next();
			List attributes = element.attributes();
			for (int i = 0; i < attributes.size(); i++) {
				Attribute attribute = attributes.get(i);
				if ("service".equals(attribute.getText())) {
					System.out.println(element.getName() + "  :  " + element.getText());
				}
			}
		}
	}
	
	
	
	
	
	
 
	
}

 

你可能感兴趣的:(htppClient调用webservice,并解析返回的xml字符串)