httpclient如何获取请求参数

目的

见另一篇博客代码需求
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;
}

结语

推荐下我的开源作品 PiperChat
PiperChat 是一款简洁高效的即时通讯服务,提供多种技术供开发者选择,帮助开发者快速构建高并发的即时通讯服务。
帮助企业快速接入 IM系统 / 聊天室系统 / 客服系统 等其他实时消息类系统,如果对你有用请求给我一个Star呀!

发送 POST 请求时,可以通过设置请求体来传递参数。如果参数是整数类型,可以将其转换为字符串类型并设置到请求体中。以下是使用 Apache HttpClient 发送带有整数参数的 POST 请求的示例代码: ``` import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.net.URI; import java.util.ArrayList; import java.util.List; public class HttpClientPostExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("param1", "123")); // 构建请求体 HttpEntity entity = new StringEntity("param1=123", ContentType.APPLICATION_FORM_URLENCODED); // 构建请求 URI URI uri = new URIBuilder() .setScheme("http") .setHost("example.com") .setPath("/api") .build(); // 构建 POST 请求 HttpPost httpPost = new HttpPost(uri); httpPost.setEntity(entity); // 执行请求 CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity responseEntity = response.getEntity(); String result = EntityUtils.toString(responseEntity); System.out.println(result); } finally { response.close(); } } } ``` 在上面的示例代码中,我们将整数参数设置为字符串类型,并将其放入请求体中。在构建请求体时,我们使用了 ContentType.APPLICATION_FORM_URLENCODED 类型,该类型表示请求体中包含 URL 编码的表单参数。在执行请求后,我们将响应结果转换为字符串并打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值