httpclient如何获取请求参数

原创
2020/06/30 21:58
阅读数 1.3K

目的

见另一篇博客代码需求
https://blog.csdn.net/GY325416/article/details/81412078

如何获取前台get请求的参数

/** * 返回get方法 填充前台传送来的参数 * * @param uri 要请求的接口地址 * @param request 前台请求过来后 controller层请求对象 * @author piper * @data 2018/7/3 11:19 */
HttpGet getMethod(String uri, HttpServletRequest request) {
    try {
        URIBuilder builder = new URIBuilder(uri);
        Enumeration<String> enumeration = request.getParameterNames();
        //将前台的参数放到我的请求里面
        while (enumeration.hasMoreElements()) {
            String nex = enumeration.nextElement();
            builder.setParameter(nex, request.getParameter(nex));
        }
        return new HttpGet(builder.build());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return null;
}

如何获取post请求的参数

post请求时可能传的是form表单,也可能是json数据

如何判断前台传送的是json数据?通过request对象的ContentType请求头可以判断
如果是json,Content-Type应该是application-json;charset=utf-8,所以获取请求头判断就行了

/** * 返回post方法 * * @param uri 要请求的地址 * @param request 前台请求对象 * @author piper * @data 2018/7/3 11:19 */
HttpPost postMethod(String uri, HttpServletRequest request) {
    StringEntity entity = null;
    if (request.getContentType().contains("json")) {
        entity = jsonData(request);  //填充json数据
    } else {
        entity = formData(request);  //填充form数据
    }
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setHeader("Content-Type", request.getHeader("Content-Type"));
    httpPost.setEntity(entity);
    return httpPost;
}

获取post请求的form表单数据

/** * 处理post请求 form数据 填充form数据 * * @param request 前台请求 * @author piper * @data 2018/7/17 18:05 */
public UrlEncodedFormEntity formData(HttpServletRequest request) {
    UrlEncodedFormEntity urlEncodedFormEntity = null;
    try {
        List<NameValuePair> pairs = new ArrayList<>();  //存储参数
        Enumeration<String> params = request.getParameterNames();  //获取前台传来的参数
        while (params.hasMoreElements()) {
            String name = params.nextElement();
            pairs.add(new BasicNameValuePair(name, request.getParameter(name)));
        }
        //根据参数创建参数体,以便放到post方法中
        urlEncodedFormEntity = new UrlEncodedFormEntity(pairs, request.getCharacterEncoding());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return urlEncodedFormEntity;
}

获取post请求的json数据

/** * 处理post请求 json数据 * * @param request 前台请求 * @author piper * @data 2018/7/17 18:05 */
public StringEntity jsonData(HttpServletRequest request) {
    InputStreamReader is = null;
    try {
        is = new InputStreamReader(request.getInputStream(), request.getCharacterEncoding());
        BufferedReader reader = new BufferedReader(is);
        //将json数据放到String中
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        //根据json数据创建请求体
        return new StringEntity(sb.toString(), request.getCharacterEncoding()); 
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部