You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haojing/src/com/customization/dito/sendtodo/HttpRequestUtil.java

138 lines
4.1 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.customization.dito.sendtodo;
import com.sun.jersey.core.util.Base64;
import okhttp3.*;
import weaver.general.BaseBean;
import java.io.IOException;
public class HttpRequestUtil extends BaseBean{
private int retryTimes = 2;
/***
*
* @param dataJson
* @return
*/
public String doPostByAuth(String portal_todourl,String dataJson,String auth){
//"http://172.16.25.133/portal-web/centerTodo/sync"
String zhjkbs = "com.customization.dito.sendtodo.HttpRequestUtil.doPostByAuth";
BaseBean bb = new BaseBean();
bb.writeLog("doPostByAuth--start:"+System.currentTimeMillis());
SendPortalErrorUtil sendPortalErrorUtil = new SendPortalErrorUtil();
String authorization = "Basic "+new String(Base64.encode(auth));
bb.writeLog("authorization:"+authorization);
bb.writeLog("portal_todourl:"+portal_todourl);
bb.writeLog("dataJson:"+dataJson);
bb.writeLog("auth:"+auth);
String msgData = "" ;
try {
OkHttpClient client = new OkHttpClient().newBuilder()
.addInterceptor(new HttpRequestUtil.OkhttpInterceptor(retryTimes,dataJson))
.retryOnConnectionFailure(false).build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, dataJson);
Request request = new Request.Builder()
.url(portal_todourl)
.method("POST", body)
.addHeader("Authorization", authorization)
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
bb.writeLog("doPostByAuth--end:"+System.currentTimeMillis());
int code = response.code();
String bodyMsg = response.body().string();
bb.writeLog("response.code():"+code);
bb.writeLog("response.body():"+bodyMsg);
if(code == 200){
msgData = bodyMsg;
return msgData;
}else {
try{
bb.writeLog("HttpRequestUtil--e:"+bodyMsg);
String jdmc = "接口第1次调用";
sendPortalErrorUtil.doRecordPortalErrorInfo4Phase2("1","",jdmc,"1",dataJson,String.valueOf(code),bodyMsg,zhjkbs,"");
}catch (Exception e){
e.printStackTrace();
bb.writeLog("doPostByAuth-e:"+e);
}
for (int i =0;i<retryTimes;i++){
response = client.newCall(request).execute();
code = response.code();
bodyMsg = response.body().string();
bb.writeLog("response.code()"+i+":"+code);
bb.writeLog("response.body()"+i+":"+bodyMsg);
if(code == 200){
msgData = bodyMsg;
break;
}else{
try{
String jdmc = "接口第"+(i+2)+"次调用";
bb.writeLog("HttpRequestUtil--e:"+bodyMsg);
sendPortalErrorUtil.doRecordPortalErrorInfo("1","","","1",dataJson,String.valueOf(code),bodyMsg);
}catch (Exception e){
e.printStackTrace();
bb.writeLog("doPostByAuth-e:"+e);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
bb.writeLog("HttpRequestUtil--e:"+e);
}
bb.writeLog("msgData:"+msgData);
return msgData;
}
public static class OkhttpInterceptor implements Interceptor {
// 最大重试次数
private int maxRentry;
private String dataJson;
private SendPortalErrorUtil sendPortalErrorUtil = new SendPortalErrorUtil();
public OkhttpInterceptor(int maxRentry,String dataJson) {
this.maxRentry = maxRentry;
this.dataJson = dataJson;
}
@Override
public Response intercept(Chain chain) throws IOException {
/* 递归 2次下发请求如果仍然失败 则返回 null ,但是 intercept must not return null.
* 返回 null 会报 IllegalStateException 异常
* */
return retry(chain, 0);//这个递归真的很舒服
}
Response retry(Chain chain, int retryCent) {
Request request = chain.request();
Response response = null;
BaseBean bb = new BaseBean();
try {
// System.out.println("第" + (retryCent + 1) + "次执行发http请求.");
response = chain.proceed(request);
} catch (Exception e) {
bb.writeLog("OkhttpInterceptor--e:"+e);
sendPortalErrorUtil.doRecordPortalErrorInfo("1","","","1",dataJson,"500",e.getMessage());
if (maxRentry > retryCent) {
return retry(chain, retryCent + 1);
}
} finally {
return response;
}
}
}
}