package com.xxxx.xxx.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

public class HttpUtil {

/**
* 发起https请求并获取结果
*
* @param requestUrl
* 请求地址
* @param requestMethod
* 请求方式(GET、POST)
* @param outputStr
* 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl,
String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpUrlConn = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestProperty(“content-type”, “text/html”);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if (“GET”.equalsIgnoreCase(requestMethod))
httpUrlConn.connect();

// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes(“UTF-8”));
outputStream.close();
}

// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, “utf-8”);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);

String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
// jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
org.jeecgframework.core.util.LogUtil
.info(“Weixin server connection timed out.”);
} catch (Exception e) {
//e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info(“https request error:{}”
+ e.getMessage());
}finally{
try {
httpUrlConn.disconnect();
}catch (Exception e) {
e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info(“http close error:{}”+ e.getMessage());
}
}
return jsonObject;
}

}

 

 

 

 

 

———————————————-

/**
* 剩余预约号数接口
* @param businessAppoint
* @param request
* @param response
* @return
* @throws ParseException
*/
@ResponseBody
@RequestMapping(params = “leftNumber”,produces = “application/json; charset=utf-8”)
public String leftNumber(BusinessAppoint businessAppoint,HttpServletRequest request,HttpServletResponse response) throws ParseException{
//实例化AjaxJson对象
JSONObject json=new JSONObject();
try {
// String businessTypeId = request.getParameter(“businessTypeId”);
//测试用的开始
// String appointDate2 = request.getParameter(“appointDate”);
// String appointDate = appointDate2.replaceAll(“-“, “”);
// businessAppoint.setAppointTime(appointDate2);
//测试用的结束
String appointDate = businessAppoint.getAppointTime().replaceAll(“-“, “”); //把-去掉
//用http://api.goseek.cn/ 提供的节假日API接口判断date是否是节假日
JSONObject jsonObject = HttpUtil.httpRequest(“http://api.goseek.cn/Tools/holiday?date=” + appointDate, “GET”, null);
if(jsonObject != null){
Map<String, Object> map = new HashMap<String, Object>();
//返回数据:{“code”:10000,”data”:2} 工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2
int data = (int) jsonObject.get(“data”);
if(0 == data){ //工作日
//查询设定的可预约号数
List<AppointNumber> list = appointNumberService.queryAll();
AppointNumber appointNumber = list.get(0);
// BusinessType appointNumber = businessTypeService.queryById(businessTypeId);
int amNumber = appointNumber.getAmNumber(); //上午可预约数
int pmNumber = appointNumber.getPmNumber(); //下午可预约数
//查询上午已预约的号数
businessAppoint.setAmPm(“上午”);
List<BusinessAppoint> amList = businessAppointService.queryListByWhere(businessAppoint);
int amLeft = amNumber – amList.size();//上午午剩余号数
//查询下午已预约的号数
BusinessAppoint businessAppoint2 = new BusinessAppoint();
businessAppoint2.setAppointTime(businessAppoint.getAppointTime());
businessAppoint2.setAmPm(“下午”);
List<BusinessAppoint> pmList = businessAppointService.queryListByWhere(businessAppoint2);
int pmLeft = pmNumber – pmList.size();//下午剩余号数
map.put(“amLeft”, amLeft);
map.put(“pmLeft”, pmLeft);
json.put(“code”, “200”);
json.put(“msg”, “获取成功”);
json.put(“data”, map);
}else{ //非工作日
json.put(“code”, “200”);
json.put(“msg”, “获取成功”);
map.put(“amLeft”, 0);
map.put(“pmLeft”, 0);
json.put(“data”, map);
}
}
} catch (Exception e) {
e.printStackTrace();
json.put(“code”, “20004”);
json.put(“msg”, “获取失败”);
}
return json.toJSONString();
}

版权声明:本文为zwcblogs原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zwcblogs/articles/9150704.html