|
|
@ -46,6 +46,7 @@ public class HttpTool { |
|
|
|
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) { |
|
|
|
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) { |
|
|
|
cookieStore.put(httpUrl.host(), list); |
|
|
|
cookieStore.put(httpUrl.host(), list); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<Cookie> loadForRequest(HttpUrl httpUrl) { |
|
|
|
public List<Cookie> loadForRequest(HttpUrl httpUrl) { |
|
|
|
List<Cookie> cookies = cookieStore.get(httpUrl.host()); |
|
|
|
List<Cookie> cookies = cookieStore.get(httpUrl.host()); |
|
|
|
return cookies != null ? cookies : new ArrayList<Cookie>(); |
|
|
|
return cookies != null ? cookies : new ArrayList<Cookie>(); |
|
|
@ -55,11 +56,17 @@ public class HttpTool { |
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
|
@Data |
|
|
|
public static class HttpToolConfig { |
|
|
|
public static class HttpToolConfig { |
|
|
|
/** 请求超时时间 */ |
|
|
|
/** |
|
|
|
|
|
|
|
* 请求超时时间 |
|
|
|
|
|
|
|
*/ |
|
|
|
private int connectTimeoutSeconds = 10; |
|
|
|
private int connectTimeoutSeconds = 10; |
|
|
|
/** http读取超时时间 */ |
|
|
|
/** |
|
|
|
|
|
|
|
* http读取超时时间 |
|
|
|
|
|
|
|
*/ |
|
|
|
private int readTimeoutSeconds = 10; |
|
|
|
private int readTimeoutSeconds = 10; |
|
|
|
/** http写超时时间 */ |
|
|
|
/** |
|
|
|
|
|
|
|
* http写超时时间 |
|
|
|
|
|
|
|
*/ |
|
|
|
private int writeTimeoutSeconds = 10; |
|
|
|
private int writeTimeoutSeconds = 10; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -92,25 +99,7 @@ public class HttpTool { |
|
|
|
* @throws IOException |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException { |
|
|
|
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException { |
|
|
|
Request.Builder requestBuilder; |
|
|
|
Request.Builder requestBuilder = buildRequestBuilder(url, form, method); |
|
|
|
if (METHOD_GET.equalsIgnoreCase(method)) { |
|
|
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); |
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : form.entrySet()) { |
|
|
|
|
|
|
|
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
requestBuilder = new Request.Builder() |
|
|
|
|
|
|
|
.url(urlBuilder.build()) |
|
|
|
|
|
|
|
.get(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : form.entrySet()) { |
|
|
|
|
|
|
|
paramBuilder.add(entry.getKey(), entry.getValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
FormBody formBody = paramBuilder.build(); |
|
|
|
|
|
|
|
requestBuilder = new Request.Builder() |
|
|
|
|
|
|
|
.url(url) |
|
|
|
|
|
|
|
.method(method, formBody); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 添加header
|
|
|
|
// 添加header
|
|
|
|
addHeader(requestBuilder, header); |
|
|
|
addHeader(requestBuilder, header); |
|
|
|
|
|
|
|
|
|
|
@ -125,6 +114,47 @@ public class HttpTool { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, String method) { |
|
|
|
|
|
|
|
switch (method) { |
|
|
|
|
|
|
|
case "get": |
|
|
|
|
|
|
|
return new Request.Builder() |
|
|
|
|
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
|
|
|
|
.get(); |
|
|
|
|
|
|
|
case "head": |
|
|
|
|
|
|
|
return new Request.Builder() |
|
|
|
|
|
|
|
.url(buildHttpUrl(url, form)) |
|
|
|
|
|
|
|
.head(); |
|
|
|
|
|
|
|
case "put": |
|
|
|
|
|
|
|
return new Request.Builder() |
|
|
|
|
|
|
|
.url(url) |
|
|
|
|
|
|
|
.put(buildFormBody(form)); |
|
|
|
|
|
|
|
case "delete": |
|
|
|
|
|
|
|
return new Request.Builder() |
|
|
|
|
|
|
|
.url(url) |
|
|
|
|
|
|
|
.delete(buildFormBody(form)); |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return new Request.Builder() |
|
|
|
|
|
|
|
.url(url) |
|
|
|
|
|
|
|
.post(buildFormBody(form)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static HttpUrl buildHttpUrl(String url, Map<String, String> form) { |
|
|
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); |
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : form.entrySet()) { |
|
|
|
|
|
|
|
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return urlBuilder.build(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static FormBody buildFormBody(Map<String, String> form) { |
|
|
|
|
|
|
|
FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
for (Map.Entry<String, String> entry : form.entrySet()) { |
|
|
|
|
|
|
|
paramBuilder.add(entry.getKey(), entry.getValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return paramBuilder.build(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 提交表单,并且上传文件 |
|
|
|
* 提交表单,并且上传文件 |
|
|
|
* |
|
|
|
* |
|
|
|