Java实现HTTP Get请求

Java实现HTTP Get请求importorg apache http HttpEntity importorg apache http client methods CloseableHtt importorg apache http client methods HttpGet importorg apache http impl client CloseableHtt

import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.stereotype.Component; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; / * @author yangxin_ryan * Http 发送请求 */ @Component public class HttpClientUtil { / * 模拟发送url Get 请求 * @param url * @return */ public String requestByGetMethod(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); StringBuilder entityStringBuilder = null; try { HttpGet get = new HttpGet(url); CloseableHttpResponse httpResponse = null; httpResponse = httpClient.execute(get); try { HttpEntity entity = httpResponse.getEntity(); entityStringBuilder = new StringBuilder(); if (null != entity) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"), 8 * 1024); String line = null; while ((line = bufferedReader.readLine()) != null) { entityStringBuilder.append(line + "/n"); } } } finally { httpResponse.close(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return entityStringBuilder.toString(); } public static void main(String args[]) { HttpClientUtil httpClientUtil = new HttpClientUtil(); String url = "http://ip:8088/ws/v1/cluster/metrics"; String res = null; try { res = httpClientUtil.requestByGetMethod(url).split("/n")[0]; System.out.println(res); } catch (Exception e) { e.printStackTrace(); } } } 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/226953.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月16日 下午10:18
下一篇 2026年3月16日 下午10:18


相关推荐

  • 最新版JDK、Eclipse安装教程

    摘要:本文详细介绍了在Windows下如何下载当前最新版的JDK及Eclipse以及具体安装步骤,并附软件安装包,最后介绍改变窗口风格、字体调整以及eclipse代码编辑缩进线(IndentGuide)插件的安装,使其编辑界面更加美观整洁。其要点如下:软件下载安装步骤界面优化前言   &am

    2022年4月7日
    254
  • jboss 下载_JbusDriver

    jboss 下载_JbusDriver如下地址栏里有JBOSS的所有版本的下载文件:http://sourceforge.net/projects/jboss/files/JBoss/ 大家只需到里面下载自己所需的就可以了 在本文中,我JBoss下载的版本为:JBOSS5.0Beta4。下载地址:http://www.jboss.org/jbossas/downloads/

    2022年10月4日
    5
  • esid是什么意思_es unassigned

    esid是什么意思_es unassignedESN(ElectronicSerialNumbers):电子序列号。在CDMA系统中,是鉴别一个物理硬件设备唯一的标识。也就是说每个手机都用这个唯一的ID来鉴别自己,就跟人的身份证一样。一个ESN有32bits,也就是32/8=4bytes。ESN用8位的16进制来表示,如0x801EA066。随着CDMA移动设别的增多,ESN已经不够用了,所以推出了位数更多的MEID。M…

    2022年8月30日
    5
  • softreference 回收_reference stacks

    softreference 回收_reference stacksSoftReference和WeakReferenceJava和Android内存优化的两个类:SoftReference和WeakReferencePostedon2010-10-2200:55charley_yang阅读(436)评论(0)编辑收藏  如果你想写一个Java程序,观察某对象什么时候会被垃圾收集的执行绪清除,你必须要用一个re

    2025年10月5日
    4
  • java逻辑删除代码_MybatisPlus实现逻辑删除功能

    java逻辑删除代码_MybatisPlus实现逻辑删除功能逻辑删除你有没有见过某些网站进行一些删除操作之后,你看不到记录了但是管理员却能够查看到。这里就运用到了逻辑删除。什么是逻辑删除?逻辑删除的本质是修改操作,所谓的逻辑删除其实并不是真正的删除,而是在表中将对应的是否删除标识(deleted)或者说是状态字段(status)做修改操作。比如0是未删除,1是删除。在逻辑上数据是被删除的,但数据本身依然存在库中。对应的SQL语句:updateusers…

    2022年6月2日
    117
  • sql数据库unique的用法_mysql中的date数据类型

    sql数据库unique的用法_mysql中的date数据类型摘自帮助:uniqueidentifier全局唯一标识符(GUID)。注释uniqueidentifier数据类型的列或局部变量可用两种方法初始化为一个值:使用NEWID函数。将字符串常量转换为如下形式(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,其中每个x是0-9或a-f范围内的一个十六进制的数字)。例如,6F9619FF-8B86-D011-B…

    2025年10月7日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号