当前位置:首页 > 开发 > 编程语言 > Json > 正文

http请求测试实例(采用fastjson解析)

发表于: 2015-06-07   作者:bijian1013   来源:转载   浏览:
摘要:         在实际开发中,我们经常会去做http请求的开发,下面则是如何请求的单元测试小实例,仅供参考。 import java.util.HashMap; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import

        在实际开发中,我们经常会去做http请求的开发,下面则是如何请求的单元测试小实例,仅供参考。

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;
    }
}

 

PS:依赖jar包见附件。

http请求测试实例(采用fastjson解析)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号