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的更多相关文章

  1. java成神之——注释修饰符

    注释修饰符 自定义注释 元注释 通过反射在runtime访问注释 内置注释 多注释实例 错误写法 使用容器改写 使用@Repeatable元注释 注释继承 使用反射获取注释 获取类的注释 获取方法的注 ...

  2. java成神之——线程操作

    线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...

  3. java成神之——文件IO

    文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStrea ...

  4. java成神之——ImmutableClass,null检查,字符编码,defaultLogger,可变参数,JavaScriptEngine,2D图,类单例,克隆,修饰符基本操作

    ImmutableClass null检查 字符编码 default logger 函数可变参数 Nashorn JavaScript engine 执行脚本文件 改变js文件输出流 全局变量 2D图 ...

  5. java成神之——集合框架之ArrayList,Lists,Sets

    集合 集合种类 ArrayList 声明 增删改查元素 遍历几种方式 空集合 子集合 不可变集合 LinkedList Lists 排序 类型转换 取交集 移动元素 删除交集元素 Sets 集合特点 ...

  6. 转载_2016,Java成神初年

    原文地址:http://blog.csdn.net/chenssy/article/details/54017826 2016,Java成神初年.. -------------- 时间2016.12. ...

  7. Java成神路上之设计模式系列教程之一

    Java成神路上之设计模式系列教程之一 千锋-Feri 在Java工程师的日常中,是否遇到过如下问题: Java 中什么叫单例设计模式?请用Java 写出线程安全的单例模式? 什么是设计模式?你是否在 ...

  8. java成神之——安全和密码

    安全和密码 加密算法 公钥和私钥加密解密 生成私钥和公钥 加密数据 解密数据 公钥私钥生成的不同算法 密钥签名 生成加密随机数 基本用法 指定算法 加密对象 SealedObject Signatur ...

  9. java成神之——网络编程基本操作

    网络编程 获取ip UDP程序示例 TCP程序 结语 网络编程 获取ip InetAddress id = InetAddress.getLocalHost(); // InetAddress id ...

随机推荐

  1. Sublime Text C# 编译(csharp.sublime-build)

    制作: 1. 配置环境变量PATH C# 7.0 C:\Program Files (x86)\Microsoft Visual Studio\\Enterprise\MSBuild\15.0\Bin ...

  2. 51nod 1009 数位dp入门

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 1009 数字1的数量 基准时间限制:1 秒 空间限制:13107 ...

  3. 查看SQLServer的最大连接数

    如何查看SQLServer的最大连接数?相信很多人对个很有兴趣,一下就给出两种方法: 1. 查询服务器属性 默认服务设置为0(表示不受限制). 2. SQL查看最大连接数 这里的32767就是服务器的 ...

  4. Ceph中的容量计算与管理

    转自:https://www.ustack.com/blog/ceph%ef%bc%8drongliang/ 在部署完Ceph集群之后,一般地我们可以通过Ceph df这个命令来查看集群的容量状态,但 ...

  5. C#学习历程(一)[基础概念]

    #小摘要 >> boolean和bool差不多是一个东西,但是bool是一个基本值的类型,boolean则是对象(java与javascript中有用). Bool是Boolean的别名. ...

  6. 【机器学习】Boosting和Bagging的差别

    boosting和bagging的差别: bagging中的模型是强模型,偏差低,方差高.目标是降低方差.在bagging中,每个模型的bias和variance近似相同,但是互相相关性不太高,因此一 ...

  7. Django --- celery异步任务与RabbitMQ模块

    一 RabbitMQ 和 celery 1 celery Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务, ...

  8. java调用cmd执行maven命令

    一.原理介绍 Java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后封闭命令窗口. cmd /k di ...

  9. Pythond 读写HDF5文件

    HDF(Hiearchical Data Format)是一种针对大量数据进行组织和存储的文件格式,可以存储不同类型的图像和数码数据的文件格式,并且可以在不同类型的机器上传输. HDF是美国国家高级计 ...

  10. Django 碎片集合

    命令行创建Django项目 熟记建立django命令:django-admin startproject xx   (start   project) 目录介绍 manage.py 文件是用来管理文件 ...