package cn.com.yitong.wdph.util;

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

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.com.yitong.ares.error.AresRuntimeException;
import cn.com.yitong.ares.net.NetConst;

/**
* 简易http通信,发往指定地址
*
* @author
*/
public class WdphHttpSendUtils {

private static Logger logger = LoggerFactory.getLogger(WdphHttpSendUtils.class);

private static int connectTimeOut = 5000;

private static int readTimeOut = 90000;

public static JSONObject sendHttp(JSONObject request, String url) {
logger.debug("-简易http通信,发往指定地址-run--");
// ----------http请求配置----------
HttpURLConnection httpURLConnection = null;
// out部分
OutputStream outputStream = null;
// in部分
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
// ----------设置请求属性信息----------
setHttpConnection(httpURLConnection);
if (logger.isDebugEnabled()) {
logger.debug("request headers:{}", httpURLConnection.getRequestProperties());
}
// 获取URLConnection对象对应的输出流
outputStream = httpURLConnection.getOutputStream();
// 发送请求参数
if (logger.isDebugEnabled()) {
logger.debug("request body:{}", request.toJSONString());
}
outputStream.write(request.toJSONString().getBytes());
outputStream.flush();
// 获取URLConnection对象对应的输入流
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, NetConst.UTF_8);
bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer sb = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
JSONObject jsb = JSONObject.parseObject(sb.toString());
if (logger.isDebugEnabled()) {
logger.debug("接收到渠道端返回的响应,响应报文为" + jsb.toJSONString());
}
return jsb;
} catch (Exception e) {
logger.error("调用渠道服务异常-->地址:{}\n 异常错误栈:\n{}", url, e);
throw new AresRuntimeException("net.connect.error", url);
} finally {
// ----------资源释放----------
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(inputStreamReader);
IOUtils.closeQuietly(bufferedReader);
if (null != httpURLConnection) {
try {
httpURLConnection.disconnect();
} catch (Exception e) {
logger.warn("http释放资源失败,", e);
}
}
}
}

/**
* 设置请求属性信息
*
* @param httpConn
* @throws ProtocolException
*/
private static void setHttpConnection(HttpURLConnection httpConn) throws ProtocolException {
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(connectTimeOut);
httpConn.setReadTimeout(readTimeOut);
httpConn.setRequestProperty("Connection", "keep-alive");
httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpConn.setRequestProperty("Accept", "application/json");
httpConn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.803.0 Safari/535.1");
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
}
}