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);
}
}