1.背景要求

发送Get请求,但是请求参数要放在请求body内,所以经过多方查证后整理出来以下代码。

2.POM依赖

        <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.2</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.5</version></dependency>

3.发送Get请求工具类

package com.example.demo.util;import com.example.demo.entity.ReqParamEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.util.EntityUtils;public class HttpUtil {public static String sendJsonByGetReq(String url, String param, String encoding) throws Exception {String body = "";//创建httpclient对象CloseableHttpClient client = HttpClients.createDefault();HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(url);HttpEntity httpEntity = new StringEntity(param, ContentType.APPLICATION_JSON);httpGetWithEntity.setEntity(httpEntity);//执行请求操作,并拿到结果(同步阻塞)CloseableHttpResponse response = client.execute(httpGetWithEntity);//获取结果实体HttpEntity entity = response.getEntity();if (entity != null) {//按指定编码转换结果实体为String类型body = EntityUtils.toString(entity, encoding);}//释放链接response.close();return body;}public static void main(String[] args) {String url = "http://localhost:8080/receiveGetReq";Gson gson = new GsonBuilder().create();ReqParamEntity reqParamEntity = new ReqParamEntity();reqParamEntity.setUserName("请求参数在请求body中");reqParamEntity.setPassWord("发送Get请求");String reqParams = gson.toJson(reqParamEntity);try {String result1 = sendJsonByGetReq (url, reqParams, "utf-8");System.out.println (result1);} catch (Exception e) {e.printStackTrace ( );}}}

4.工具类关联类

package com.example.demo.util;import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;import java.net.URI;public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {private final static String METHOD_NAME = "GET";@Overridepublic 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));}}

5.验证是否请求成功

package com.example.demo.controller;import com.example.demo.entity.ReqParamEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.web.bind.annotation.*;@RestController
public class DemoController {@GetMapping("/receiveGetReq")public String receiveGetReq(@RequestBody ReqParamEntity reqParamEntity){Gson gson = new GsonBuilder().create();System.out.println("reqParamEntity="+gson.toJson(reqParamEntity));return "请求成功";}}

6.总结

请求成功

Java发送Http请求之——发送请求参数在Body中Get请求相关推荐

  1. java中的 请求体_在spring mvc test中访问请求体和请求头

    我创建了一个spring boot应用程序,这就是我的控制器的样子 . 我使用postman在请求体中发送json,在请求头中发送一个字符串,然后进一步散列json并将其与请求头获取的字符串进行比较 ...

  2. vue 请求参数数组_vue中get请求如何传递数组参数

    前言: vue中在与后端进行数据交互时,使用axios发送请求,不做配置直接使用get请求传递数组类型参数的时候,后端是无法接收数据的,需要对axios一些简单的配置才能让后端完美的接收数组 1.问题 ...

  3. java httpclient 重定向_httpclient 中post请求重定向

    背景:使用httpclient 的post请求进行登录,需要重定向登录,请求重定向后的地址 在httpclient中post请求不像get请求自己可以重定向,实现方式是 判断post请求返回码是否是3 ...

  4. java 获取自定义参数类型_Springboot中使用自定义参数注解获取 token 中用户数据...

    使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...

  5. angular发布代码有缓存_如何在Angular应用程序中执行请求?

    全文共5358字,预计学习时长16分钟 来源:Pexels 本文将讨论如何在Angular应用程序中执行请求. 1. 使用拦截器来装饰请求 2. HttpClient 与HttpBackend的对比 ...

  6. http java编程_Java编程:发送HTTP请求到服务器

    当Java程序需要向服务器发送请求或读取服务器数据时,使用URLConnection类是比较好的选择.URLConnection类封装了与服务器互动操作的方法,通过它可以建立与服务器的远程连接,检查服 ...

  7. Java使用hutool工具类发送http请求

    Java使用hutool工具类发送网络请求 引入jar包 pom.xml添加以下hutool依赖 <dependency><groupId>cn.hutool</grou ...

  8. python 字典字符串转字典——urllib.request.Request发送get,post请求,发送json参数

    1.eval方法即可[字典字符串转字典] file_content = eval(file_content) 2.urllib.request.Request发送post请求,发送json参数 fro ...

  9. jsp怎么接受ajax请求参数,通过ajax发送JSON并通过JSP中的请求获取参数

    我需要通过ajax(使用Jquery)发送JSON对象,并通过JSP(服务器端)中的请求对象获取所有参数.通过ajax发送JSON并通过JSP中的请求获取参数 我的JS代码: var request ...

最新文章

  1. Java中@Override的作用
  2. rust军用船指令_Rust基础学习笔记(五):Cargo与Crates.io
  3. Python开发以太坊智能合约指南(web3.py)
  4. jena dataset 增删改查
  5. linux学习笔记-10.解压与压缩
  6. 利刃 MVVMLight 1:MVVMLight介绍以及在项目中的使用
  7. 【javascript权威指南】类型转换
  8. OJ1046: 奇数的乘积(C语言)
  9. element ui里dialog关闭后清除验证条件
  10. 简单java程序_简单的Java程序
  11. python对文件操作的相关函数_第六章、Python文件操作
  12. 解决IE8不能安装支付宝控件问题
  13. iOS视频播放器开发
  14. PHP中MD5函数效率
  15. 天下武功唯快不破------实验吧
  16. Pyscript,使用Python编写前端脚本
  17. 镭速关于高速数据传输的介绍!
  18. JavaScrip笔记心得(持续更新)
  19. 【PC工具】文字文本朗读工具,语音朗读软件,TTS语音合成
  20. com.netflix.client.ClientException: load balancer doer not hava available server for client: XXX 的报错

热门文章

  1. 7nm芯片在路上,AMD又倒逼英特尔?
  2. Linux- 硬件查询——lspci/lsusb/lsblk/blkid/lscpu/hdparm/sdparm/smart/dmidecode
  3. 测试开发之路--喷喷埋雷的事,吵吵代码的情
  4. 阿里三面 Android 研发岗,竟然挂在了性能优化上……
  5. 追忆我的2008-爱上读书篇
  6. android获取刘海屏状态栏高度,Android刘海屏全面屏底部导航栏的适配
  7. Java HashMap1.7头插法扩容时出现循环链表 1.8换成尾插法
  8. Python编程题(1)——淘淘摘苹果
  9. 数据清洗必须会的一些方法 - sql篇
  10. C++:见缝插针双人对战