直接上代码
1.为xml 和string,byte 相互转换
package xxxxx;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class XmlUtil {
public String xmltoString(File file) {
String string = null;
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
string = document.asXML();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return string;
}
public Document StringtoXml(String string){
Document document=null;
SAXReader reader = new SAXReader();
try {
document=reader.read(new ByteArrayInputStream(string.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return document;
}
public Document BytetoXml(byte[] b){
Document document=null;
SAXReader reader = new SAXReader();
try {
document=reader.read(new ByteArrayInputStream(b));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return document;
}
}
2.模拟客户端发送请求和解析服务端下发数据 并匹配相应数据
package com.xxx.test;
import java.io.File;
import static org.junit.Assert.assertEquals;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.dom4j.Document;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import com.app.util.EncryptUtil;
import com.juyun.test.util.XmlUtil;
/**
* 测试获取主页接口
* @author yaven
*
*/
public class TestHomePage {
static HttpClient httpClient;
private String url = "http://xxxxx.aspx";
XmlUtil xUtil = new XmlUtil();
private File file = new File("JBPHomePage.xml");
@Before
public void setUpBeforeEveryTime() throws Exception{
System.out.println("+++setUpBefore");
System.out.println("flush cache...");
System.out.println("===========================");
}
@Test
public void testHomePage() throws Exception{
httpClient = new HttpClient();
EntityEnclosingMethod postMethod = new PostMethod();
String string = xUtil.xmltoString(file);
byte[] request = string.getBytes("UTF-8");
byte[] requestcode =EncryptUtil.getInstance().encode(request);//加密
RequestEntity requestEntity = new ByteArrayRequestEntity(requestcode);
postMethod.setRequestEntity(requestEntity);
postMethod.setPath(url);
postMethod.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30*1000);
httpClient.executeMethod(postMethod);
byte[] responsecode =postMethod.getResponseBody();
byte[] response =EncryptUtil.getInstance().decode(responsecode);
Document document = xUtil.BytetoXml(response);
String values = document.getStringValue();
System.out.print(values);
assertEquals("-1", values.substring(0, 2));
}
@AfterClass //执行一次
public static void tearDownAfterClassOnce() throws Exception {
System.out.println("===========================");
System.out.println("tearDownAfterClass");
}
}