This commit is contained in:
tanghc
2019-09-02 19:37:22 +08:00
parent 0cafb5829b
commit e47cd0e002
8 changed files with 197 additions and 24 deletions

View File

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