代码改变世界

【转】HTTP协议两种提交参数的方式Form-data和raw

2016-06-07 10:34  xiaoluosun  阅读(3619)  评论(0编辑  收藏  举报

原文:http://www.cnblogs.com/zhangfei/p/5099036.html 

 

HTTP协议的接口测试中,使用到最多的就是GET请求与POST请求,其中POST请求有FORM参数提交请求与RAW请求,下面我将结合HttpClient来实现一下这三种形式:

一.GET请求: GET请求时,参数一般是写在链接上的,代码如下:

 1 public void get(String url){
 2     CloseableHttpClient httpClient = null;
 3     HttpGet httpGet = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();     
 7         httpGet = new HttpGet(url);
 8         httpGet.setConfig(requestConfig);
 9         CloseableHttpResponse response = httpClient.execute(httpGet);
10         HttpEntity httpEntity = response.getEntity();
11         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
12     } catch (ClientProtocolException e) {
13         e.printStackTrace();
14     } catch (IOException e) {
15         e.printStackTrace();
16     }finally{
17         try {
18             if(httpGet!=null){
19                 httpGet.releaseConnection();
20             }
21             if(httpClient!=null){
22                 httpClient.close();
23             }
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }
28 }

 

如果想把参数不写在链接上,单独的传进去,则可以这样:

 1 public void get(String url, Map<String, String> params){
 2     CloseableHttpClient httpClient = null;
 3     HttpGet httpGet = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
 7         String ps = "";
 8         for (String pKey : params.keySet()) {
 9             if(!"".equals(ps)){
10                 ps = ps + "&";
11             }
12             ps = pKey+"="+params.get(pKey);
13         }
14         if(!"".equals(ps)){
15             url = url + "?" + ps;
16         }
17         httpGet = new HttpGet(url);
18         httpGet.setConfig(requestConfig);
19         CloseableHttpResponse response = httpClient.execute(httpGet);
20         HttpEntity httpEntity = response.getEntity();
21         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
22     } catch (ClientProtocolException e) {
23         e.printStackTrace();
24     } catch (IOException e) {
25         e.printStackTrace();
26     }finally{
27         try {
28             if(httpGet!=null){
29                 httpGet.releaseConnection();
30             }
31             if(httpClient!=null){
32                 httpClient.close();
33             }
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37     }
38 }

 

二. POST请求的表单提交方式,代码如下:

 1 public void post(String url, Map<String, String> params){
 2     CloseableHttpClient httpClient = null;
 3     HttpPost httpPost = null;
 4     try {
 5         httpClient = HttpClients.createDefault();
 6         RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
 7         httpPost = new HttpPost(url);
 8         httpPost.setConfig(requestConfig);
 9         List<NameValuePair> ps = new ArrayList<NameValuePair>();
10         for (String pKey : params.keySet()) {
11             ps.add(new BasicNameValuePair(pKey, params.get(pKey)));
12         }
13         httpPost.setEntity(new UrlEncodedFormEntity(ps));
14         CloseableHttpResponse response = httpClient.execute(httpPost);
15         HttpEntity httpEntity = response.getEntity();
16         System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
17     } catch (ClientProtocolException e) {
18         e.printStackTrace();
19     } catch (IOException e) {
20         e.printStackTrace();
21     }finally{
22         try {
23             if(httpPost!=null){
24                 httpPost.releaseConnection();
25             }
26             if(httpClient!=null){
27                 httpClient.close();
28             }
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32     }
33 }

三. POST请求的RAW参数传递:

public void post(String url, String body){
    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;
    try {
        httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
        httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        httpPost.setEntity(new StringEntity(body));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        System.out.println(EntityUtils.toString(httpEntity,"utf-8"));
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            if(httpPost!=null){
                httpPost.releaseConnection();
            }
            if(httpClient!=null){
                httpClient.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}