package com.rs.zero.crc.common.http;
import com.rs.zero.crc.modulars.common.constants.SysConstantConf;
import com.xiaoleilu.hutool.util.ObjectUtil;
import okhttp3.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;
/**
* HTTP 请求utils
*
* @author RS.LSY Created by IntelliJ IDEA.
* @since 2017/05/02 20:11
*/
public class HttpUtils {
private static Log log = LogFactory.getLog(HttpUtils.class);
private static final MediaType JSON = MediaType.parse("application/mode; charset=utf-8");
/**
* get 请求.
* <p>连接10s超时,写10s,读30s</p>
*
* @param url 请求URL
* @return 请求结果
* @throws IOException 异常
*/
public static String okHttpGet(String url) throws IOException {
log.info("okHttpPost 请求url:" + url);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(SysConstantConf.okHttp().getInteger("connectTimeout"), TimeUnit.SECONDS)
.writeTimeout(SysConstantConf.okHttp().getInteger("writeTimeout"), TimeUnit.SECONDS)
.readTimeout(SysConstantConf.okHttp().getInteger("readTimeout"), TimeUnit.SECONDS)
.build();
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
/**
* OKHttP POST 请求.
* <p>连接10s超时,写10s,读30s</p>
*
* @param url 请求URL
* @param json 请求参数
* @return 请求结果
* @throws IOException IO异常
*/
public static String okHttpPost(String url, String json) throws IOException, InterruptedException {
int maxTimes = SysConstantConf.okHttp().getInteger("maxTimes");
int sleepTime = SysConstantConf.okHttp().getInteger("sleepTime");
int initTimes = 0;
Response response = ssss(url, json);
while (!response.isSuccessful() && maxTimes >= initTimes) {
initTimes++;
response = ssss(url, json);
Thread.sleep(sleepTime);
log.info("正在进行第" + initTimes + "次重试");
log.info("Unexpected code:" + response);
}
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
private static Response ssss(String url, String json) throws IOException {
log.info("okHttpPost 请求url:" + url);
log.info("okHttpPost 请求json:" + json);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(SysConstantConf.okHttp().getInteger("connectTimeout"), TimeUnit.SECONDS)
.writeTimeout(SysConstantConf.okHttp().getInteger("writeTimeout"), TimeUnit.SECONDS)
.readTimeout(SysConstantConf.okHttp().getInteger("readTimeout"), TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.build();
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
return client.newCall(request).execute();
}
/**
* 获取body数据.
*
* @param request HttpServletRequest
* @return body数据
* @throws IOException IOException
*/
public static String getPostBody(HttpServletRequest request) throws IOException {
StringBuilder sb = new StringBuilder();
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String s;
while ((s = br.readLine()) != null) {
sb.append(s);
}
if (ObjectUtil.isNotNull(is)) {
is.close();
}
if (ObjectUtil.isNotNull(isr)) {
isr.close();
}
if (ObjectUtil.isNotNull(br)) {
br.close();
}
return sb.toString();
}
}