http请求测试实例(采用fastjson解析)
2017-10-09 19:05
1136 查看
import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.alibaba.fastjson.JSONObject; public class TTest { private String serverURL = "http://serviceAddress/system"; private String address = "/address/queryInfo"; private String username = "lisi"; private String password = "123"; @Before public void setUp() throws Exception { } @Test public void queryInfo_test() throws Exception { Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("queryName", "张三"); paramMap.put("age", 30); String paramJson = JSONObject.toJSONString(paramMap); String result = httpConnectionWithAuth(serverURL, address, paramJson, username, password); //String result = httpConnectionWithNoAuth(serverURL, address, paramJson); JSONObject object = JSONObject.parseObject(result); Assert.assertEquals(object.get("result"), true); } /** * 带认证的请求 */ private String httpConnectionWithAuth(String url, String addr, String json, String un, String pw) throws Exception { System.out.println("请求参数:\r\n" + json); System.out.println("请求地址:" + url + addr); HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(url + "//j_security_check"); NameValuePair[] param = { new NameValuePair("j_username", un), new NameValuePair("j_password", pw), new NameValuePair("SMAUTHREASON", "0") }; postMethod.setRequestBody(param); client.executeMethod(postMethod); postMethod.releaseConnection(); GetMethod method = new GetMethod(url + "/do/LoginController/login"); client.executeMethod(method); long s = System.currentTimeMillis(); postMethod = new PostMethod(url + addr); postMethod.setRequestBody(json); postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); client.executeMethod(postMethod); long t = System.currentTimeMillis(); String result = postMethod.getResponseBodyAsString(); System.out.println("执行时长:" + (t - s) + "ms"); System.out.println("返回结果:" + JSONObject.parseObject(result)); postMethod.releaseConnection(); return result; } /** * 无需认证的请求 */ private String httpConnectionWithNoAuth(String url, String addr, String json) throws Exception { System.out.println("请求参数:\r\n" + json); System.out.println("请求地址:" + url + addr); HttpClient client = new HttpClient(); long s = System.currentTimeMillis(); PostMethod postMethod = new PostMethod(url + addr); postMethod.setRequestBody(json); postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8"); client.executeMethod(postMethod); long t = System.currentTimeMillis(); String result = postMethod.getResponseBodyAsString(); System.out.println("执行时长:" + (t - s) + "ms"); System.out.println("返回结果:" + JSONObject.parseObject(result)); postMethod.releaseConnection(); return result; } }
相关文章推荐
- AsyncHttpClient 网络请求+ fastJson解析数据
- Python发送http请求解析返回json的实例
- http get/post请求及json解析
- Retrofit发送网络请求和解析json实例
- postman 测试http post的json请求
- Http请求解析Json与图片(AlertDialog)提示框
- Jmeter压力测试-http的post请求实例
- Netty学习笔记之一(Netty解析简单的Http Post Json 请求)
- android通过httpClient请求获取JSON数据并且解析
- springMVC4(4)json与对象互转实例解析请求响应数据转换器
- Http请求解析Json与图片(AlertDialog)提示框
- 一个完整的通过encoding/json信息传递格式,采用http.POST利用进行与服务器通信,并对返回结果进行解析处理的典型案例
- http请求返回并解析json数据
- Demo XML 、 JSON 解析 AND 网络HTTP请求
- Loadrunner http JSON接口请求进行测试
- Http请求返回json解析java.lang.IndexOutOfBoundException错误
- fastJson解析复杂的json字符串,经测试已经成功解析
- 使用apache的httpclient发送post请求传送和解析json
- httpclient 请求http数据,json转map的实例
- http请求返回并解析json数据