获取当天是否节假日
程序员文章站
2022-05-08 09:48:48
...
package com.cn.baidu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Holiday
{
/**
* @param urlAll :请求接口
* @param httpArg :参数
* @return 返回结果
*/
public static String request(String httpArg)
{
String httpUrl = "http://www.easybots.cn/api/holiday.php";
// String httpUrl = "http://apis.baidu.com/xiaogg/holiday/holiday";
HttpClient httpClient = new HttpClient();
GetMethod method = new GetMethod(httpUrl+"?"+httpArg);
String response = "";
try
{
// 需要添加的header数据
List<Header> headers = new ArrayList<Header>();
Header header = new Header();
header.setName("apikey");
header.setValue("7bd4e99adcc28e337cea79191fef87fc");
headers.add(header);
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK)
{
// 获取到的回执信息
InputStream resStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
StringBuffer resBuffer = new StringBuffer();
String resTemp = "";
while ((resTemp = br.readLine()) != null)
{
resBuffer.append(resTemp);
}
response = resBuffer.toString();
}
}
catch (HttpException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
method.releaseConnection();
}
return response;
}
public static void main(String[] args)
{
// 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
String httpArg = "d="+"2016-11-20".replaceAll("-", "");
String jsonResult = request(httpArg);
System.out.println(jsonResult);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Holiday
{
/**
* @param urlAll :请求接口
* @param httpArg :参数
* @return 返回结果
*/
public static String request(String httpArg)
{
String httpUrl = "http://www.easybots.cn/api/holiday.php";
// String httpUrl = "http://apis.baidu.com/xiaogg/holiday/holiday";
HttpClient httpClient = new HttpClient();
GetMethod method = new GetMethod(httpUrl+"?"+httpArg);
String response = "";
try
{
// 需要添加的header数据
List<Header> headers = new ArrayList<Header>();
Header header = new Header();
header.setName("apikey");
header.setValue("7bd4e99adcc28e337cea79191fef87fc");
headers.add(header);
httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(method);
if (statusCode == HttpStatus.SC_OK)
{
// 获取到的回执信息
InputStream resStream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(resStream));
StringBuffer resBuffer = new StringBuffer();
String resTemp = "";
while ((resTemp = br.readLine()) != null)
{
resBuffer.append(resTemp);
}
response = resBuffer.toString();
}
}
catch (HttpException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
method.releaseConnection();
}
return response;
}
public static void main(String[] args)
{
// 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
String httpArg = "d="+"2016-11-20".replaceAll("-", "");
String jsonResult = request(httpArg);
System.out.println(jsonResult);
}
}
上一篇: Java HashMap透析
下一篇: SQL 学习笔记
推荐阅读