两个Http联调测试工具类

package com.thirdPlat.ys.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import com.alibaba.fastjson.JSONObject;

public class ClaimThirdCommonTest {
	public static void main(String[] args) throws Exception {
		//http://10.29.27.60:8161/serviceapi/claim/sendMessage
		//String url = "http://localhost:8888/servlet/thirdPlat/gcCommonServlet?";//本地
		//String url = "http://10.29.27.60:8161/serviceapi/claim/sendMessage";//第六套联调
		
		//英仕联调
		String url = "http://10.0.100.185:6001/platform/correction.do";
		
		String path = System.getProperty("user.dir");//user.dir指定了当前的路径 
		String requestXml = readXML(path + "/src/com/thirdPlat/ys/test/001claim.xml");
		System.out.println("请求参数:");
		System.out.println(requestXml);
		String responseXml = getResponseXml(requestXml, url);
		System.out.println("\n返回参数:");
		System.out.println(responseXml);
	}
	
	/**
	 * 获取响应报文
	 * @param request
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getResponseXml(String requestXml, String url) throws Exception {
		StringBuilder buffer = new StringBuilder();
		InputStream inputStream = null;
		BufferedReader reader = null;
		String strMessage = "";
		HttpClient httpClient = new HttpClient();
		HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();
		// 设置连接超时时间(单位毫秒)
		managerParams.setConnectionTimeout(10000*60);
		// 设置读数据超时时间(单位毫秒)
		managerParams.setSoTimeout(60000*60);
		PostMethod postMethod = new PostMethod(url);
  	    // 将请求参数XML的值放入postMethod中
		try {
			postMethod.setRequestEntity(new StringRequestEntity(requestXml,"application/xml", "UTF-8"));
			//获取http状态返回代码 200-服务器成功返回网页
			int statusCode=0;
			try {
				statusCode = httpClient.executeMethod(postMethod);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (statusCode != 200) {
				throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
			}
			inputStream = postMethod.getResponseBodyAsStream();
			reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			while ((strMessage = reader.readLine()) != null){
				buffer.append(strMessage);
			}
		} catch (Exception ex) {
			throw new IllegalStateException(ex.toString());
		} finally {
			//释放连接
			postMethod.releaseConnection();
		}
		return buffer.toString();
	}

	@SuppressWarnings("resource")
	public static String readXML(String xmlpath) {
		InputStream in = null;
		Reader reader = null;
		try {
			int aa;
			in = new FileInputStream(xmlpath);
			reader = new InputStreamReader(in, "UTF-8");
			StringBuffer sb = new StringBuffer("");
			while ((aa = reader.read()) != -1) {
				sb.append((char) aa);
			}
			return sb.toString();
		} catch (Exception localException) {
		}
		return null;
	}
}

/**
 * 
 */
package com.thirdPlat.ys.test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import com.chinainsurance.sysframework.common.util.ExceptionUtils;



/**
 * @author Alex
 *
 */
public class LocalClaimTestInterface {

	public static void main(String[] args) throws Exception{
		
		try {
			String url =getURL(1);//0-本地 1-测试环境 2-冒烟环境
			url =url+"/platform/correction.do";
			//手工发起投保保存或合并分组请使用Auto.xml
			String reqStr = getRequestXML("001claim");//报文名称:类型+产品代码 产品类型:T-投保 E-批改
			//dopost(reqStr,url);
			String charset="UTF-8";
			int connTimeOut=3000;
			int readTimeOut=40000;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			int status = post2(url, reqStr, charset, connTimeOut,readTimeOut, bos);
			String responseXML = new String(bos.toByteArray(), charset);
			System.out.print("Re:"+responseXML);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * 获取URL
	 * @param index:0-本地 1-测试环境
	 * @return
	 * @throws Exception
	 */
	public static String getURL(int index)throws Exception{
		String [] urls =new String[]{"http://10.6.12.97:8888","http://10.0.100.185:6001","http://10.0.100.183:7004","http://10.12.46.95:8001"};
		return urls[index];
	}
	public static void dopost(String reqStr,String url) throws Exception{
		PostMethod pm = new PostMethod(url+"//claim/GeECMSServlet.do");
		pm.setRequestEntity(new StringRequestEntity(reqStr.toString(), "text/xml","UTF-8"));
		HttpClient client = new HttpClient();
		HttpConnectionManagerParams httparams = client.getHttpConnectionManager().getParams();
		// 设置连接超时时间(单位毫秒)
		httparams.setConnectionTimeout(30000);
		// 设置读数据超时时间(单位毫秒)
		httparams.setSoTimeout(120000);
		int res = client.executeMethod(pm);
		if (res == HttpStatus.SC_OK) {		
			printResponseInfo(pm.getResponseBodyAsStream());
		}	
	}
	public static int post2(String uri, String xml, String charset, int connTimeOut, int readTimeOut, OutputStream recv)
	throws Exception {
		InputStream inputStream = null;
		PostMethod pm = new PostMethod(uri);
		try {
			pm.setRequestHeader("Connection", "Close");
			pm.setRequestEntity(new StringRequestEntity(xml, "text/xml", charset));
			HttpClient client = new HttpClient();
			HttpConnectionManagerParams httparams = client.getHttpConnectionManager().getParams();
			// 设置连接超时时间(单位毫秒)
			httparams.setConnectionTimeout(connTimeOut);
			// 设置读数据超时时间(单位毫秒)
			httparams.setSoTimeout(readTimeOut);
			int res = client.executeMethod(pm);
			if (res == HttpStatus.SC_OK) {
				inputStream = pm.getResponseBodyAsStream();
				//printResponseInfo(pm.getResponseBodyAsStream());
				byte[] buff = new byte[1024 * 100];
				int len = 0;
				while ((len = inputStream.read(buff)) > 0) {
					recv.write(buff, 0, len);
				}
				recv.flush();
			}
			return res;
		} finally {
			pm.releaseConnection();
			if (inputStream != null) {
				inputStream.close();
			}
		}
   }
	/**
	 * 获取请求报文
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String getRequestXML(String fileName) throws Exception{
		String requestXML = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream(0);
		InputStream in = null;
		
		try {
			byte[] buff = new byte[1024];
			int len = 0;
			in = new FileInputStream("D:/ALLRTC_GQ/JW_20210304_Nom_Dev/claim2JwIfc/claim2JwIfc/src/com/thirdPlat/ys/test/001claim.xml");
			while ((len = in.read(buff)) > 0) {
				bos.write(buff, 0, len);
			}
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {

				}
			}
			bos.close();
		}
		requestXML = new String(bos.toByteArray(), "UTF-8");
		return requestXML;
	}
	public static void printResponseInfo(InputStream inputStream) throws Exception{
		String respXML = null;
		StringBuilder buffer = new StringBuilder();
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
	    while ((respXML = reader.readLine()) != null) {
            buffer.append(respXML).append("\n");
 	    }
//	    ExceptionUtils.printInfoLog(buffer.toString());
	    System.out.println(buffer.toString());
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值