Home> Java> javaTutorial> body text

Get whether the day is a holiday

巴扎黑
Release: 2016-12-03 11:56:43
Original
2731 people have browsed it

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: request interface
* @param httpArg: parameter
* @return return result
*/
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

headers = new ArrayList
();
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);
}

}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!