Java工具类之在服务器发送HTTP请求
使用之前首先添加maven依赖或者是jar包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
下面给出工具类代码,一个简单发送Get和Post请求的例子
public class HttpUtils { /* *发送简单post请求 */ public static JSONObject post(String url) { HttpPost post = new HttpPost(url); return getResult(post); } /* *发送带Header的post请求 */ public static JSONObject post(String url, Map<String, String> map) { HttpPost post = new HttpPost(url); if (!map.isEmpty()) { Set<Map.Entry<String, String>> entrys = map.entrySet(); for (Map.Entry<String, String> entry : entrys) { post.setHeader(entry.getKey(), entry.getValue()); } } return getResult(post); } /* *发送带Header的get请求 */ public static JSONObject get(String url, Map<String, String> map) { HttpGet get = new HttpGet(url); if (!map.isEmpty()) { Set<Map.Entry<String, String>> entrys = map.entrySet(); for (Map.Entry<String, String> entry : entrys) { get.setHeader(entry.getKey(), entry.getValue()); } } return getResult(get); } /* *发送简单的get请求 */ public static JSONObject get(String url) { HttpGet get = new HttpGet(url); return getResult(get); } /* *发送请求方法,请求响应为JSONObject */ private static JSONObject getResult(HttpRequestBase requestBase) { CloseableHttpClient httpClient = HttpClients.createDefault(); String result = null; try { result = EntityUtils.toString(httpClient.execute(requestBase).getEntity()); httpClient.close(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { return new JSONObject(JSON.parseObject(result)); } } /* *当请求响应为String时 */ public static String getString(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet get = new HttpGet(url); String result = null; try { result = EntityUtils.toString(httpClient.execute(get).getEntity()); httpClient.close(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { return result; } } }
使用之前首先添加maven依赖或者是jar包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
下面给出工具类代码,一个简单发送Get和Post请求的例子
public class HttpUtils { /* *发送简单post请求 */ public static JSONObject post(String url) { HttpPost post = new HttpPost(url); return getResult(post); } /* *发送带Header的post请求 */ public static JSONObject post(String url, Map<String, String> map) { HttpPost post = new HttpPost(url); if (!map.isEmpty()) { Set<Map.Entry<String, String>> entrys = map.entrySet(); for (Map.Entry<String, String> entry : entrys) { post.setHeader(entry.getKey(), entry.getValue()); } } return getResult(post); } /* *发送带Header的get请求 */ public static JSONObject get(String url, Map<String, String> map) { HttpGet get = new HttpGet(url); if (!map.isEmpty()) { Set<Map.Entry<String, String>> entrys = map.entrySet(); for (Map.Entry<String, String> entry : entrys) { get.setHeader(entry.getKey(), entry.getValue()); } } return getResult(get); } /* *发送简单的get请求 */ public static JSONObject get(String url) { HttpGet get = new HttpGet(url); return getResult(get); } /* *发送请求方法,请求响应为JSONObject */ private static JSONObject getResult(HttpRequestBase requestBase) { CloseableHttpClient httpClient = HttpClients.createDefault(); String result = null; try { result = EntityUtils.toString(httpClient.execute(requestBase).getEntity()); httpClient.close(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { return new JSONObject(JSON.parseObject(result)); } } /* *当请求响应为String时 */ public static String getString(String url) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet get = new HttpGet(url); String result = null; try { result = EntityUtils.toString(httpClient.execute(get).getEntity()); httpClient.close(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (ClientProtocolException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { return result; } } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
字节码
这篇文章主要内容来自"深入java虚拟机",刚毕业那会太急功近利,这块离业务代码太远就没细看。这次花点时间整理一下,加深对字节码的认识。 类文件结构 “一次编写,到处运行”表达的是“与平台无关”。如何做到“与平台无关”?Sun公司以及其他虚拟机提供商发布了许多可以运行在各种平台上的虚拟机,这些虚拟机都可以载入和执行同一种平台无关的字节码(class文件)。 Java虚拟机的野心远不止做到“与平台无关”,Java虚拟机还做到“语言无关性”。Java虚拟机支持其他语言的代码通过对应语言的编译器,编译成字节码,从而运行在Java虚拟机之上。 Class文件是一组以8位字节为基础单元的二进制流,各个数据项目严格按照顺序紧凑地排列在Class文件之中,中间没有添加任何分隔符。Class文件格式采用一种类似于C语言结构体的伪结构来存储数据,这种伪结构只有两种数据类型:无符号数 和 表。 表1:Class文件格式定义 名称 类型 数量 描述 magic u4 1 class文件的识别码,固定,为“0xCAFFBABE” minor_version u2 1 次版本号,JDK1.7_u91,其中91就...
- 下一篇
Dockerfile配置APM监控实现Java容器的性能监控
版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问我的博客 https://blog.csdn.net/smooth00/article/details/80858394 通过Dockerfile可以用来构建容器镜像,我们一般也是通过这种方式来构建一个Tomcat应用服务容器,如果要实现对容器中的Tomcat服务(或是其他Java应用)进行APM(应用性能管理)监控,就需要我们在容器中放置javaagent并做相关配置,而在已生成的容器中修改配置不符合容器管理的规范,所以我们建议在发布镜像时就将javaagent植入,这样在生成容器时就可以通过环境变量参数来决定是否开启监控。 (请持续关注smooth的博客:https://blog.csdn.net/smooth00,另外有关容器的文章还可以关注:《搭建Jmeter容器集群平台》、《以容器部署Ganglia并监控Hadoop集群》、《Rancher及Docker快速上手指南(一)》、《Rancher及Docker快速上手指南(二)》、《Rancher及Docker快速上手指南(三)》、《Jenkins自动化部署容器》) 以下...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Red5直播服务器,属于Java语言的直播服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS6,CentOS7官方镜像安装Oracle11G
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装