http请求get和post代码

本文详细介绍了HTTP中的GET和POST请求的用法,提供了Java实现的示例代码。GET通常用于获取资源,而POST用于提交数据。在POST请求中,数据被包含在请求体中,而在GET请求中,数据作为URL的一部分。示例代码展示了如何设置请求头、添加参数并执行请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于http里面的get和post请求


    /**
     * 发送post请求
     * @param url  路径
     * @param jsonObject  参数(json类型)
     * @param encoding 编码格式
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String send(String url, JSONObject jsonObject, String encoding,HashMap<String,String> map,String param) throws ParseException, IOException{
        String body = "";

        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);

        //装填参数
        StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        //设置参数到请求对象中
        httpPost.setEntity(s);
        System.out.println("请求地址:"+url);
//        System.out.println("请求参数:"+nvps.toString());

        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
//        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
        httpPost.setHeader("ClientId","com.kdxf.esb.fyjk");
        httpPost.setHeader("OperationCode",map.get("OperationCode"));
        httpPost.setHeader("LoginCode","esbuser");
        httpPost.setHeader("LoginPassword","123456");
        if (param != null) {
            httpPost.setHeader("Authorization", param);

        }
        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = client.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }



    /**
     * 发送post请求
     * @param url  路径
     * @param param  参数(json类型)
     * @param
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String httpGet(String url,String param,HashMap<String,String> map) throws UnsupportedEncodingException {

        // 获取连接客户端工具
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse httpResponse=null;
        String finalString = null;
        HttpGet httpGet = new HttpGet(url);
        /**公共参数添加至httpGet*/
        httpGet.setHeader("Accept","*/*");
        httpGet.setHeader("Accept-Encoding","gzip, deflate, br");
        httpGet.setHeader("Cache-Control","no-cache");
        httpGet.setHeader("Connection", "keep-alive");
        httpGet.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpGet.setHeader("ClientId","com.kdxf.esb.fyjk");
        httpGet.setHeader("OperationCode",map.get("OperationCode"));
        httpGet.setHeader("LoginCode","esbuser");
        httpGet.setHeader("LoginPassword","123456");

         /**业务参数*/
          if(param !=null){
              httpGet.setHeader("Authorization", param);
          }
        try {
            httpResponse = httpClient.execute(httpGet);
            System.out.println("===================="+httpResponse);
            HttpEntity entity = httpResponse.getEntity();
            finalString= EntityUtils.toString(entity, "UTF-8");
            try {
                httpResponse.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return finalString;
    }

插入完成,这个可以在请求头里面加数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值