Json格式请求测试工具类
package com.ifc.test.claimthirdcommon;
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;
public class ClaimThirdCommonTest {
public static void main(String[] args) throws Exception {
String url = "http://0.0.00.181:7006/claim/servlet/thirdPlat/gcCommonServlet?";
String path = System.getProperty("user.dir");
String requestXml = readXML(path + "/src/com/ifc/test/claimthirdcommon/002payee.json");
System.out.println(requestXml);
String responseXml = getResponseXml(requestXml, url);
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();
}
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;
}
}
json报文格式
{
"名称1": "属性1",
"名称2": "属性2"
...
}