Java中发送http的get请求,body带请求参数如何实现,如何从cookie取值

672 阅读1分钟

1、需求描述:

这次也是需要对接客户系统中的一个http的接口,没想到它居然是一个get请求body中带参数,你要就是post请求中带参数咯,真的是设计得奇葩,但是是客户的系统没法改,就只能硬着头皮上了呀。

2、解决方案:

1)、首先从cookie中取得我们需要的参数

@RequestMapping(value="/name",method= RequestMethod.POST)
    public void list(HttpServletRequest request, HttpServletResponse response, @RequestParam("data") String data){
        try{
            JSONObject paramsJson = JSONObject.parseObject(data);
            //取cookie中的code的值
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if ("code".equals(cookie.getName())){
                    paramsJson.put("code", cookie.getValue());
                }
            }

            JSONObject resultJson = autoLogonService.IcName(paramsJson);
            responseOutWithJson(response, resultJson);
        }catch (Exception e) {
            // TODO: handle exception
            log.error("出现异常:",e);
        }
    }

2)、发送get请求

@Override
    public JSONObject IcName(JSONObject param) {
        JSONObject resultJson = JsonUtil.resultData();

        try {
            JSONObject code = new JSONObject();
            code.put("code", param.getString("code"));
            String body = "";
            //创建httpclient对象
            HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(URI.create("http://IP:port/usersession/getUserInfoByCode"));
            //get请求的body参数
            HttpEntity httpEntity = new StringEntity(code.toString(), ContentType.APPLICATION_FORM_URLENCODED);
            httpGetWithEntity.setEntity(httpEntity);
            //执行请求操作,并拿到结果(同步阻塞)
            CloseableHttpClient client = HttpClientBuilder.create().build();
            CloseableHttpResponse response = client.execute(httpGetWithEntity);

            //获取结果实体
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                //按指定编码转换结果实体为String类型
                body = EntityUtils.toString(entity, "UTF-8");
            }
            //释放链接
            response.close();
            JSONObject json = JSONObject.parseObject(body);

            if ("200".equals(json.getString("code")) && "true".equals(json.getString("code")) && "操作成功".equals(json.getString("code"))){
                resultJson.put("msg", "成功");
                resultJson.put("code", "0000");
                resultJson.put("result", true);
                return resultJson;
            }
            resultJson.put("msg", "失败");
            resultJson.put("code", "0001");
            resultJson.put("result", false);
            return resultJson;
        }catch (Exception e){
            e.printStackTrace();
            resultJson.put("msg", "失败");
            resultJson.put("code", "0001");
            resultJson.put("result", false);
            return resultJson;
        }
    }

下面是new出来的HttpGetWithEntity对象

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import java.util.Date;
/**
 * @author huang
 * @description
 * @date 2022/11/9 15:59
 */
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    private final static String METHOD_NAME = "GET";

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }

    public HttpGetWithEntity() {
        super();
    }

    public HttpGetWithEntity(final URI uri) {
        super();
        setURI(uri);
    }

    HttpGetWithEntity(final String uri) {
        super();
        setURI(URI.create(uri));
    }
}


前端js定时执行方法
function name(){}
setInterval("name()","6000");//每6秒执行一次


3、最后:

这样应该完美解决了http的get请求,body带请求参数的问题了,希望可以帮助到你。