当前位置: 首页 > news >正文

一个用java的get请求

java发送一个get请求,请求参数class=yanfa,使用Authorization认证,在Request Header里填充Authorization: Bearer {token}进行请求认证,token为:sadagdagdgdgfagfd ,另外在Header里补充App标识,X-Client-Tag:plmm,然后获取返回的json内容

方法1 (HttpURLConnection)

public static void classOwner(String class) {String url = "http://baidu.com/api/wenxinyiyanproject?class=" + class;String Token = "sadagdagdgdgfagfd";String clientTag = "plmm";try {URL obj = new URL(url);HttpURLConnection con = (HttpURLConnection) obj.openConnection();// 设置请求方法为GETcon.setRequestMethod("GET");// 添加Authorization头con.setRequestProperty("Authorization", "Bearer " + beToken);// 添加X-Client-Tag头con.setRequestProperty("X-Client-Tag", clientTag);int responseCode = con.getResponseCode();final String responseMessage = con.getResponseMessage();if (responseCode == HttpURLConnection.HTTP_OK) {BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));String inputLine;StringBuilder response = new StringBuilder();while ((inputLine = in.readLine()) != null) {response.append(inputLine);}in.close();System.out.println("response" + response.toString());} else {System.out.println("Error: " + responseCode);}} catch (Exception e) {System.out.println("发送GET请求出现异常:" + e.getMessage());}}

方法2(HttpClients)

public class HttpGetExample {public static void main(String[] args) throws IOException {String url = "http://baidu.com/api/wenxinyiyanproject?class=yanfa";// 创建HttpClient对象try (CloseableHttpClient httpClient = HttpClients.createDefault()) {// 创建HttpGet对象HttpGet httpGet = new HttpGet(url);// 添加Authorization认证httpGet.setHeader("Authorization", "Bearer sadagdagdgdgfagfd");// 添加X-Client-Tag标识httpGet.setHeader("X-Client-Tag", "plmm");// 发送GET请求HttpResponse response = httpClient.execute(httpGet);// 获取响应码int statusCode = response.getStatusLine().getStatusCode();System.out.println("Response Code: " + statusCode);// 获取响应内容try (InputStream inputStream = response.getEntity().getContent()) {String responseBody = new String(inputStream.readAllBytes());// 打印响应内容System.out.println(responseBody);}}}
}

方法3(RestTemplate)

public class HttpGetExample {public static void main(String[] args) {String url = "http://baidu.com/api/wenxinyiyanproject?class=yanfa";// 创建RestTemplate对象RestTemplate restTemplate = new RestTemplate();// 设置请求头HttpHeaders headers = new HttpHeaders();headers.set("Authorization", "Bearer sadagdagdgdgfagfd");headers.set("X-Client-Tag", "plmm");HttpEntity<String> entity = new HttpEntity<>(null, headers);try {// 发送GET请求,并接收响应ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);// 获取响应码int statusCode = response.getStatusCodeValue();System.out.println("Response Code: " + statusCode);// 获取响应内容String responseBody = response.getBody();// 打印响应内容System.out.println(responseBody);} catch (Exception e) {e.printStackTrace();}}
}
http://www.lryc.cn/news/180565.html

相关文章:

  • 作为SiteGPT替代品,HelpLook的优势是什么?
  • uni-app:实现页面效果2(canvas绘制,根据页面宽度调整元素位置)
  • 【24种设计模式】责任链模式(Chain of Responsibility Pattern)
  • 微信小程序一对多个页面间传递数据进行通信,事件触发的实现方法
  • 软件测试之Python基础学习
  • 模块化编程+LCD1602调试工具——“51单片机”
  • 【Linux】UDP的服务端 + 客户端
  • 德国自动驾驶卡车公司【Fernride】完成1900万美元A轮融资
  • 实现水平垂直居中的十种方式
  • 头条号热点采集工具-头条号热文采集软件
  • 了解”变分下界“
  • Andriod 简单控件
  • Substructure‑aware subgraph reasoning for inductive relation prediction
  • 古诗词学习鉴赏APP设计与实现(源码+lw+部署文档+讲解等)
  • 深度学习与python theano
  • 【算法优选】双指针专题——贰
  • AI智能电话机器人实用吗
  • 网络爬虫--伪装浏览器
  • C/C++程序的内存开辟
  • 【Java 进阶篇】JDBC DriverManager 详解
  • 2023年Linux总结常用命令
  • Mybatis3详解 之 全局配置文件详解
  • 力扣-345.反转字符串中的元音字母
  • 643. 子数组最大平均数I(滑动窗口)
  • Java 21 新特性:虚拟线程(Virtual Threads)
  • 18scala笔记
  • 【LeetCode周赛】LeetCode第365场周赛
  • 响应式设计的实现方式
  • PHP 反序列化漏洞:__PHP_Incomplete_Class 与 serialize(unserialize($x)) !== $x;
  • TempleteMethod