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 {
String url = "http://10.0.100.185:6001/platform/correction.do";
String path = System.getProperty("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);
}
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);
try {
postMethod.setRequestEntity(new StringRequestEntity(requestXml,"application/xml", "UTF-8"));
int statusCode=0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (Exception e) {
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;
public class LocalClaimTestInterface {
public static void main(String[] args) throws Exception{
try {
String url =getURL(1);
url =url+"/platform/correction.do";
String reqStr = getRequestXML("001claim");
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();
}
}
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();
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();
}
}
}
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");
}
System.out.println(buffer.toString());
}
}