mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0
This commit is contained in:
@@ -13,6 +13,7 @@ import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -243,6 +244,51 @@ public class HttpTool {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求数据
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param form 请求数据
|
||||
* @param header header
|
||||
* @return 返回Response
|
||||
* @throws IOException
|
||||
*/
|
||||
public Response requestForResponse(String url, Map<String, ?> form, Map<String, String> header, HTTPMethod method) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
return httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
*
|
||||
* @param url 请求url
|
||||
* @param form 请求数据
|
||||
* @param header header
|
||||
* @return 返回文件流
|
||||
* @throws IOException
|
||||
*/
|
||||
public InputStream downloadFile(String url, Map<String, ?> form, Map<String, String> header) throws IOException {
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, HTTPMethod.GET);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
|
||||
Request request = requestBuilder.build();
|
||||
Response response = httpClient
|
||||
.newCall(request)
|
||||
.execute();
|
||||
if (response.isSuccessful()) {
|
||||
ResponseBody body = response.body();
|
||||
return body == null ? null : body.byteStream();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addHeader(Request.Builder builder, Map<String, String> header) {
|
||||
if (header != null) {
|
||||
Set<Map.Entry<String, String>> entrySet = header.entrySet();
|
||||
|
Reference in New Issue
Block a user