post请求的参数如何添加?

来源:2-4 重构HttpClientUtil-上

慕粉3200454

2018-02-07 11:12

post请求的参数如何添加?老师的视频中只是一个字符串,能否帮忙指出如何添加post方法携带的参数?谢谢老师

写回答 关注

1回答

  • qq_那时年少_0
    2018-08-01 07:23:49

    public static String httpPost(String url,List<BasicNameValuePair> list) {

    String result = null;

    RequestConfig requestConfig = RequestConfig.custom()

    .setConnectTimeout(5000)   //设置连接超时时间

    .setConnectionRequestTimeout(5000) // 设置请求超时时间

    .setSocketTimeout(5000)

    .setRedirectsEnabled(true)//默认允许自动重定向

    .build();

    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

    HttpPost httpPost = new HttpPost(url);

    httpPost.setConfig(requestConfig);

    try {

    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");

    //设置post请求参数

    httpPost.setEntity(entity);

    HttpResponse httpResponse = closeableHttpClient.execute(httpPost);

    if(httpResponse.getStatusLine().getStatusCode() == 200) {

    result = EntityUtils.toString(httpResponse.getEntity());

    }

    } catch (Exception e) {

    e.printStackTrace();

    }finally {

    try {

    closeableHttpClient.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    return result;

    }


使用java构建和维护接口自动化测试框架

初识接口自动化框架

25748 学习 · 36 问题

查看课程

相似问题