接口测试 小白的接口测试

fanqi · 2016年12月24日 · 最后由 fanqi 回复于 2016年12月25日 · 1222 次阅读

今天,小白的 get、post 请求。。。。。。

一、对于 get 请求,就是 url 和参数合并成一个接口请求地址

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class DoGetRequest {

    /*
     * get请求方式
     * @author:Me
     */
    public static String httpGetMethod(String url,Map<Object,Object> map) throws InstantiationException, IllegalAccessException {

        HttpClient httpClient = new DefaultHttpClient();
        Object value;
        Object urlInfo = "";
        for(Object key : map.keySet()){
            value = map.get(key);
            urlInfo += key+"="+value+"&";
        }
        urlInfo = urlInfo.toString().substring(0, urlInfo.toString().length()-1);        
        url = url + "?" + urlInfo.toString();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
            StringBuilder sb = new StringBuilder();
            InputStream inputStream = httpResponse.getEntity().getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            String temp = "";
            while((temp = br.readLine()) != null){
                sb.append(temp + "\n");
            }
            return sb.toString();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
        return null;        
    }
}

二、对于 post 请求,组装好参数类型,发送请求

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;

@SuppressWarnings("deprecation")
public class DoPostMethod {

    /*
     * author:ME
     */
        @SuppressWarnings("deprecation")
        public String  HttpClientPostMethod(String url,Map map) {

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            InputStream responseStream = null;
            //参数形式为JSON格式
            JSONObject parameter = new JSONObject(map);
            try {
                StringEntity entity = new StringEntity(parameter.toString());
                entity.setContentEncoding("UTF-8");    
                entity.setContentType("application/json"); //设置参数类型为JSON格式
                httpPost.setEntity(entity);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                if(httpResponse.getStatusLine().getStatusCode() == 200){
                    HttpEntity httpEntity = httpResponse.getEntity();
                    responseStream = httpEntity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
                    StringBuilder sb = new StringBuilder();
                    String temp = "";
                    while((temp = br.readLine()) != null){
                        String str = new String(temp.getBytes(), "UTF-8");
                        sb.append(str + "\n");
                    }
                    return sb.toString();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;        
        }
}
共收到 2 条回复 时间 点赞

写得不错,但有几个点遗漏了:

get 的:

  1. 没有对保留字做 url 转码,例如当参数的 key 或者 value 中含有 & 或者 = 符号,拼出来的 url 会出问题。
  2. 不大清楚 HttpGet 会不会自动做 url 编码(例如中文或者其它字符进行 url 转码),如果不会,应该在组合 url 的时候做好转码。

post 的:

  1. post 参数类型固定是 json 有点不对吧,遇到非 json 的就不行了。建议方法名说明下只能用于 json ,例如 HttpClientPostMethodWithJson
  2. 当服务端返回的状态不是 200 时,方法返回了 null 且没有任何相关日志,定位问题会比较麻烦。

最后,强烈建议使用类似 okhttpclient 之类的更高层次的封装框架代替直接用 httpclient ,省很多代码,可读性也会强不少。

嗯嗯,谢谢,目前 okhttpclient 还没看过。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册