java成神之——HttpURLConnection访问api
HttpURLConnection
访问get资源
HttpURLConnection connection = (HttpURLConnection)new URL("http://ip/test").openConnection();
int responseCode = connection.getResponseCode();
InputStream inputStream;
if (200 <= responseCode && responseCode <= 299) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
}
BufferedReader in = new BufferedReader( new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine()) != null) response.append(currentLine);
in.close();
response.toString();
访问post资源
HttpURLConnection connection = (HttpURLConnection)new URL("http://ip/test").openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
out.write("post传递的数据".getBytes());
out.close();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
in.close();
if (connection != null) connection.disconnect();
if (out != null) out.close();
if (in != null) in.close();
访问Delete资源
HttpURLConnection connection = (HttpURLConnection)new URL("http://ip/test").openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestMethod("DELETE");
connection.setDoInput(true);
Map<String, List<String>> map = connection.getHeaderFields();
StringBuilder sb = new StringBuilder();
Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, List<String>> entry = iterator.next();
sb.append(entry.getKey());
sb.append('=').append('"');
sb.append(entry.getValue());
sb.append('"');
if(iterator.hasNext()){
sb.append(',').append(' ');
}
}
System.out.println(sb.toString());
if (connection != null) connection.disconnect();
获取状态码
HttpURLConnection connection = (HttpURLConnection)new URL("http://ip/test").openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
connection.disconnect();
结语
本文章是java成神的系列文章之一
如果你想知道,但是本文没有的,请下方留言
我会第一时间总结出来并发布填充到本文
java成神之——HttpURLConnection访问api的更多相关文章
- java成神之——注释修饰符
注释修饰符 自定义注释 元注释 通过反射在runtime访问注释 内置注释 多注释实例 错误写法 使用容器改写 使用@Repeatable元注释 注释继承 使用反射获取注释 获取类的注释 获取方法的注 ...
- java成神之——线程操作
线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...
- java成神之——文件IO
文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStrea ...
- java成神之——ImmutableClass,null检查,字符编码,defaultLogger,可变参数,JavaScriptEngine,2D图,类单例,克隆,修饰符基本操作
ImmutableClass null检查 字符编码 default logger 函数可变参数 Nashorn JavaScript engine 执行脚本文件 改变js文件输出流 全局变量 2D图 ...
- java成神之——集合框架之ArrayList,Lists,Sets
集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 ...
- 转载_2016,Java成神初年
原文地址:http://blog.csdn.net/chenssy/article/details/54017826 2016,Java成神初年.. -------------- 时间2016.12. ...
- Java成神路上之设计模式系列教程之一
Java成神路上之设计模式系列教程之一 千锋-Feri 在Java工程师的日常中,是否遇到过如下问题: Java 中什么叫单例设计模式?请用Java 写出线程安全的单例模式? 什么是设计模式?你是否在 ...
- java成神之——安全和密码
安全和密码 加密算法 公钥和私钥加密解密 生成私钥和公钥 加密数据 解密数据 公钥私钥生成的不同算法 密钥签名 生成加密随机数 基本用法 指定算法 加密对象 SealedObject Signatur ...
- java成神之——网络编程基本操作
网络编程 获取ip UDP程序示例 TCP程序 结语 网络编程 获取ip InetAddress id = InetAddress.getLocalHost(); // InetAddress id ...
随机推荐
- 升级openssl 到 1.0.1s 最新版
1.下载 wget http://www.openssl.org/source/openssl-1.0.1s.tar.gz 2.解压 tar -zxf openssl-1.0.1s.tar.gz cd ...
- opencv:傅里叶变换
示例代码: #include <opencv.hpp> #include <iostream> using namespace std; using namespace cv; ...
- 5天不再惧怕多线程——第一天 尝试Thread
随笔 - 218 文章 - 1 评论 - 3819 5天不再惧怕多线程——第一天 尝试Thread 原本准备在mongodb之后写一个lucene.net系列,不过这几天用到多线程时才发现自己 ...
- linux文件组、权限等
文件所有者.所在组合其他组 --改变用户所在组 组和在oa系统中的组差不多,用户代表的好像是个体,组有点像角色的意思.不过权限的话并不是个体从组中获得,组仅仅是一个机制,进行部分文件控制与共享 ...
- 剑指offer--33.丑数
本来用数组做标志位,但是测试数据有第1500个,859963392,惹不起哦 ------------------------------------------------------------- ...
- 视图框架:Spring MVC 4.0(2)
在<springMVC4(7)模型视图方法源码综合分析>一文中,我们介绍了ModelAndView的用法,它会在控制层方法调用完毕后作为返回值返回,里面封装好了我们的业务逻辑数据和视图对象 ...
- Apache Tomcat Server Options 选项说明
Apache Tomcat Server Options 选项说明 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neu ...
- Debian, Ubuntu, LinuxMint 安裝 MySQL 5.7, 5.6, 5.5
以下會示範在 Debian, Ubuntu 及 LinuxMint 分別安裝 MySQL 5.7, 5.6, 5.5 的方法. 首先按照需要的安裝的 MySQL 版本, 加入相應的 Repositor ...
- (十)js获取日期
//将日期转换成字符串格式输出 function formatDateToString(){ // 先获取对象日期 var oDate = new Date(); // 从该对象中分别拿出所需要的 年 ...
- Python之functools库
functools库用于高阶函数,指那些作用于函数或者返回其他函数的函数 functools提供方法如下: cmp_to_key 将一个比较函数转换关键字函数 partial 针对函数起作用,并且是部 ...