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 ...
随机推荐
- hive 权限:Authorization failed:No privilege 'Create' found for outputs .
创建表报错: create table test ( name string ); Authorization failed:No privilege 'Create' found for outpu ...
- opencv:摄像头和视频的读取
示例代码: #include <opencv.hpp> using namespace cv; int main() { VideoCapture Capture(); //打开默认摄像头 ...
- hdu4185
题解:每两个联通的油井建边 然后二分图最大匹配 最后答案除以2 代码: #include<cstdio> #include<cmath> #include<cstring ...
- Node.js/Python爬取网上漫画
某个周日晚上偶然发现了<火星异种>这部漫画,便在网上在线看了起来.在看的过程中图片加载很慢,而且有时候还不小心点到广告,大大延缓了我看的进度.后来想到能不能把先把漫画全部抓取到本地再去看. ...
- Django框架(三)
0627内容: 上节回顾: 1. FBV.CBV 2. 数据库操作 class UserGroup(models.Model): """ 部门 3 "" ...
- 关于iframe和div窗口中ajax请求200状态时执行的回调问题
上一篇说了在ajax回调里面处理iframe窗口的刷新问题,这一篇记录一下遇到的一个分别在iframe和div窗口中ajax请求200状态时执行的回调问题. 我们先来看一下ajax请求的写法(这里使用 ...
- Android开发技巧——写一个StepView
在我们的应用开发中,有些业务流程会涉及到多个步骤,或者是多个状态的转化,因此,会需要有相关的设计来展示该业务流程.比如<停车王>应用里的添加车牌的步骤. 通常,我们会把这类控件称为&quo ...
- Ganymed实现基本的自动化部署API
Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...
- Delphi for Android (aka Delphi XE5 aka RAD Studio XE5) has appeared
Delphi for Android (aka Delphi XE5 aka RAD Studio XE5) has appeared Blimey, that took me by surpri ...
- fedora to ubunto
Fedora to Ubuntu16.04 一.删除Fedora 由于双系统启动的时候是由linux系统做引导启动,所以在Windows下直接格式化Linux分区将导致无法启动Windows.解决办法 ...