import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.Request.Builder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClient {
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);
public OkHttpClient okHttpClient;
protected String content_json_type = "application/json";
protected static final String empty_param = "{}";
protected HttpClient(OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
}
public <R> String post(String url, R param) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).post(body).build();
return this.fire(request);
}
protected <R> String postConvertParam(R param) {
return JSON.toJSONString(param);
}
public String post(String url) {
this.paramLog("{}", url);
RequestBody body = RequestBody.create("{}", MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).post(body).build();
return this.fire(request);
}
public <R> String post(String url, R param, Map<String, String> headers) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).headers(Headers.of(headers)).post(body).build();
return this.fire(request);
}
public String get(String url) {
HttpUrl httpUrl = HttpUrl.parse(url);
if (httpUrl == null) {
return null;
} else {
this.paramLog("", url);
Request request = (new Builder()).url(url).get().build();
return this.fire(request);
}
}
public String get(String url, Map<String, String> params) {
HttpUrl paramBuild = this.getParamBuild(url, params);
if (paramBuild == null) {
return "";
} else {
Request request = (new Builder()).url(paramBuild).get().build();
return this.fire(request);
}
}
public String get(String url, Map<String, String> params, Map<String, String> headers) {
HttpUrl paramBuild = this.getParamBuild(url, params);
if (paramBuild == null) {
return "";
} else {
Request request = (new Builder()).url(paramBuild).headers(Headers.of(headers)).get().build();
return this.fire(request);
}
}
private HttpUrl getParamBuild(String url, Map<String, String> params) {
this.paramLog(JSON.toJSONString(params), url);
HttpUrl httpUrl = HttpUrl.parse(url);
if (httpUrl == null) {
log.error("解析异常url:{}", url);
return null;
} else {
okhttp3.HttpUrl.Builder httpBuilder = httpUrl.newBuilder();
if (params != null) {
params.forEach(httpBuilder::addQueryParameter);
}
return httpBuilder.build();
}
}
public <R> String put(String url, R param) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).put(body).build();
return this.fire(request);
}
public <R> String delete(String url, R param, Map<String, String> headers) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).headers(Headers.of(headers)).delete(body).build();
return this.fire(request);
}
public String delete(String url) {
this.paramLog("{}", url);
RequestBody body = RequestBody.create("{}", MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).delete(body).build();
return this.fire(request);
}
public <R> String put(String url, R param, Map<String, String> headers) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).headers(Headers.of(headers)).put(body).build();
return this.fire(request);
}
public String put(String url, Map<String, String> headers) {
this.paramLog("{}", url);
RequestBody body = RequestBody.create("{}", MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).headers(Headers.of(headers)).put(body).build();
return this.fire(request);
}
public String put(String url) {
this.paramLog("{}", url);
RequestBody body = RequestBody.create("{}", MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).put(body).build();
return this.fire(request);
}
public <R> String delete(String url, R param) {
String jsonString = this.postConvertParam(param);
this.paramLog(jsonString, url);
RequestBody body = RequestBody.create(jsonString, MediaType.parse(this.content_json_type));
Request request = (new Builder()).url(url).delete(body).build();
return this.fire(request);
}
public byte[] download(String url) {
this.paramLog("{}", url);
Request request = (new Builder()).url(url).get().build();
try {
ResponseBody body = this.okHttpClient.newCall(request).execute().body();
if (body != null) {
return body.bytes();
}
} catch (IOException var4) {
log.error("下载异常url:{}", url, var4);
}
return null;
}
public InputStream downloadOssInputStream(String url) {
this.paramLog("{}", url);
Request request = (new Builder()).url(url).header("Referer", "http://www.test.com").get().build();
try {
ResponseBody body = this.okHttpClient.newCall(request).execute().body();
if (body != null) {
return body.byteStream();
}
} catch (IOException var4) {
log.error("下载异常url:{}", url, var4);
}
return null;
}
public byte[] downloadOss(String url) {
this.paramLog("{}", url);
Request request = (new Builder()).url(url).header("Referer", "http://www.test.com").get().build();
try {
ResponseBody body = this.okHttpClient.newCall(request).execute().body();
if (body != null) {
return body.bytes();
}
} catch (IOException var4) {
log.error("下载异常url:{}", url, var4);
}
return null;
}
public String upload(String url, String fileKey, String fileName, byte[] bytes) {
return this.upload(url, fileKey, (Map)null, fileName, bytes);
}
public String upload(String url, String fileKey, Map<String, String> params, String fileName, byte[] bytes) {
this.paramLog("{}", url);
RequestBody fileBody = RequestBody.create(bytes);
okhttp3.MultipartBody.Builder builder = (new okhttp3.MultipartBody.Builder()).setType(MultipartBody.FORM).addFormDataPart(fileKey, fileName, fileBody);
if (params != null) {
params.forEach(builder::addFormDataPart);
}
RequestBody requestBody = builder.build();
Request request = (new Builder()).url(url).post(requestBody).build();
return this.fire(request);
}
public String fire(Request request) {
Call call = this.okHttpClient.newCall(request);
try {
Response response = call.execute();
return this.getResult(response);
} catch (Exception var4) {
log.error("请求异常url:{}", request.url(), var4);
return "";
}
}
public static <R> R convert(String str, TypeReference<R> typeReference) {
return str != null && !str.equals("") ? JSON.parseObject(str, typeReference.getType(), new Feature[0]) : null;
}
protected void paramLog(String param, String url) {
log.info("请求路径:{},参数:{}", url, param);
}
private void resultLog(String result) {
log.info("请求返回:{}", result);
}
private String getResult(Response response) throws IOException {
int code = response.code();
if (code != 200) {
log.error("请求异常路径:{},code:{},message:{}", new Object[]{response.request().url().toString(), response.code(), response.message()});
return "";
} else {
ResponseBody body = response.body();
if (body != null) {
String result = body.string();
this.resultLog(result);
return result;
} else {
return "";
}
}
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
import java.util.concurrent.TimeUnit;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
public class HttpClientFactory {
static Dispatcher dispatcher = new Dispatcher();
public HttpClientFactory() {
}
public static HttpClient getInstance(int connectTime, int readTimeOut) {
OkHttpClient okHttpClient = (new Builder()).connectTimeout((long)connectTime, TimeUnit.SECONDS).readTimeout((long)readTimeOut, TimeUnit.SECONDS).dispatcher(dispatcher).build();
return new HttpClient(okHttpClient);
}
public static FormHttpClient getFormHttpInstance(int connectTime, int readTimeOut) {
OkHttpClient okHttpClient = (new Builder()).connectTimeout((long)connectTime, TimeUnit.SECONDS).readTimeout((long)readTimeOut, TimeUnit.SECONDS).dispatcher(dispatcher).build();
return new FormHttpClient(okHttpClient);
}
public static HttpClient getInstance(OkHttpClient okHttpClient) {
return new HttpClient(okHttpClient);
}
static {
dispatcher.setMaxRequests(300);
dispatcher.setMaxRequestsPerHost(300);
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
import com.alibaba.fastjson.TypeReference;
import java.io.InputStream;
import java.util.Map;
import okhttp3.Request;
public class CHttpClient {
public static HttpClient httpClient = HttpClientFactory.getInstance(5, 5);
public CHttpClient() {
}
public static <P> String post(String url, P param) {
return httpClient.post(url, param);
}
public static <R, P> R post(String url, P param, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.post(url, param), typeReference);
}
public static String post(String url) {
return httpClient.post(url);
}
public static <R> R post(String url, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.post(url), typeReference);
}
public static <P> String post(String url, P param, Map<String, String> headers) {
return httpClient.post(url, param, headers);
}
public static <R, P> R post(String url, P param, Map<String, String> headers, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.post(url, param, headers), typeReference);
}
public static String get(String url, Map<String, String> params) {
return httpClient.get(url, params);
}
public static <R> R get(String url, Map<String, String> params, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.get(url, params), typeReference);
}
public static String get(String url) {
return httpClient.get(url);
}
public static <R> R get(String url, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.get(url), typeReference);
}
public static String get(String url, Map<String, String> params, Map<String, String> headers) {
return httpClient.get(url, params, headers);
}
public static <R> R get(String url, Map<String, String> params, Map<String, String> headers, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.get(url, params, headers), typeReference);
}
public static <P> String put(String url, P param, Map<String, String> headers) {
return httpClient.put(url, param, headers);
}
public static <R, P> R put(String url, P param, Map<String, String> headers, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.put(url, param, headers), typeReference);
}
public static <R> R put(String url, Map<String, String> headers, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.put(url, headers), typeReference);
}
public static <R> R put(String url, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.put(url), typeReference);
}
public static String put(String url) {
return httpClient.put(url);
}
public static <P> String delete(String url, P param, Map<String, String> headers) {
return httpClient.delete(url, param, headers);
}
public static String delete(String url) {
return httpClient.delete(url);
}
public static <R> R delete(String url, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.delete(url), typeReference);
}
public static <R, P> R delete(String url, P param, Map<String, String> headers, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.delete(url, param, headers), typeReference);
}
public static <P> String put(String url, P param) {
return httpClient.put(url, param);
}
public static <R, P> R put(String url, P param, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.put(url, param), typeReference);
}
public static <P> String delete(String url, P param) {
return httpClient.delete(url, param);
}
public static <R, P> R delete(String url, P param, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.delete(url, param), typeReference);
}
public static byte[] download(String url) {
return httpClient.download(url);
}
public static byte[] downloadOss(String url) {
return httpClient.downloadOss(url);
}
public static InputStream downloadOssInputStream(String url) {
return httpClient.downloadOssInputStream(url);
}
public static String upload(String url, String fileKey, String fileName, byte[] bytes) {
return httpClient.upload(url, fileKey, fileName, bytes);
}
public static <R> R upload(String url, String fileKey, Map<String, String> params, String fileName, byte[] bytes, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.upload(url, fileKey, params, fileName, bytes), typeReference);
}
public static String upload(String url, String fileKey, Map<String, String> params, String fileName, byte[] bytes) {
return httpClient.upload(url, fileKey, params, fileName, bytes);
}
public static <R> R upload(String url, String fileKey, String fileName, byte[] bytes, TypeReference<R> typeReference) {
return HttpClient.convert(httpClient.upload(url, fileKey, fileName, bytes), typeReference);
}
public static String fire(Request request) {
return httpClient.fire(request);
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
import java.lang.reflect.Field;
import java.util.Map;
import okhttp3.OkHttpClient;
public class FormHttpClient extends HttpClient {
private static FormHttpClient formHttpClient;
protected FormHttpClient(OkHttpClient okHttpClient) {
super(okHttpClient);
super.content_json_type = "application/x-www-form-urlencoded";
}
protected <R> String postConvertParam(R param) {
if (param instanceof String) {
return param.toString();
} else {
StringBuilder builder = new StringBuilder();
if (param instanceof Map) {
((Map)param).forEach((key, valuex) -> {
builder.append(key).append("=").append(valuex).append("&");
});
} else {
Class<?> superClass = param.getClass();
for(int i = 0; i < 5 && superClass != Object.class; ++i) {
Field[] var5 = superClass.getDeclaredFields();
int var6 = var5.length;
for(int var7 = 0; var7 < var6; ++var7) {
Field field = var5[var7];
field.setAccessible(true);
String fieldName = field.getName();
Object value;
try {
value = field.get(param);
} catch (IllegalAccessException var12) {
throw new UnsupportedOperationException("参数转换失败");
}
builder.append(fieldName).append("=").append(value).append("&");
}
superClass = superClass.getSuperclass();
}
}
return builder.substring(0, builder.length() - 1);
}
}
public static FormHttpClient getInstance() {
if (formHttpClient == null) {
formHttpClient = HttpClientFactory.getFormHttpInstance(5, 5);
}
return formHttpClient;
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59