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

}
优质内容筛选与推荐>>
1、【ExtJS实践】之二 :TreeGrid显示复选框
2、对拍
3、eclipse 总是提示文件下载
4、读取注册表
5、MyBatis(一)--环境搭建


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号