前言
在演示的功能代码中使用的请求http地址为:http://timor.tech/api/holiday/year/
接口说明:获取指定年份或年月份的所有节假日信息。默认返回当前年份的所有信息。
以上功能代码仅在使用GET请求方式时测试通过,POST等其他请求时请自行尝试。
因未设置请求头时也成功获取了响应数据,所以未在演示代码中添加请求头信息,请求失败时可自行添加请求头信息后重试
方式一:功能实现类 java.net.HttpURLConnection
请求实现代码:
package com.zhiyin.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MyTest {
public static void main(String[] args) {
test();
}
// HttpURLConnection方式
public static void test() {
String SUBMIT_METHOD_GET = "GET"; // 一定要是大写,否则请求无效
String urlStr = "http://timor.tech/api/holiday/year/"; // 请求http地址
String param = "2020"; // 请求参数
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null; // 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(urlStr);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:GET
connection.setRequestMethod(SUBMIT_METHOD_GET);
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,请求成功后获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
}
result = sbf.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 释放资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect(); // 关闭远程连接
}
System.out.println("Successfully:" + result);
}
}
方式二:功能实现类 org.apache.http.client.methods.HttpGet
maven项目中pom.xml文件里引入依赖:
org.apache.httpcomponents
httpclient
4.5.2
请求实现代码:
package com.zhiyin.test;
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 java.io.InputStream;
public class MyTest {
public static void main(String[] args) {
test();
}
// HttpGet方式
public static void test() {
try {
String urlStr = "http://timor.tech/api/holiday/year/"; // 请求http地址
String param = "2020"; // 请求参数
// 模拟(创建)一个浏览器用户
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(urlStr + param);
// httpclient进行连接
CloseableHttpResponse response = client.execute(httpGet);
// 获取内容
HttpEntity entity = response.getEntity();
// 将内容转化成IO流
InputStream content = entity.getContent();
// 输入流转换成字符串
byte[] byteArr = new byte[content.available()];
content.read(byteArr); // 将输入流写入到字节数组中
String result = new String(byteArr);
System.out.println("Successfully:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
原文:https://www.cnblogs.com/zhiyin1209/p/12599376.html