前言
在演示的功能代码中使用的请求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地址
St