分享C/S服务端接口测试 http协议下xml格式数据 上传和解析(原创)

直接上代码

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");


}
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值