导入依赖
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
工具类
package com.ly.uitls;
import okhttp3.*;
import org.springframework.util.CollectionUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
/**
* @author : ly
*/
public class OkHttpClientUtil {
private final static OkHttpClient okHttpClient = new OkHttpClient();
/**
* get请求
*
* @param url
* @param headerMap
* @return
* @throws Exception
*/
public static String get(String url, Map<String, String> headerMap) throws Exception {
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach((name, value) -> builder.addHeader(name, value));
}
Request request = builder.build();
ResponseBody body = okHttpClient.newCall(request).execute().body();
return body.string();
}
/**
* @param url
* @return
*/
public static String getResult(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
try {
Response response = call.execute();
String string = response.body().string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* get 请求
*
* @param url
* @param headerMap
* @return
* @throws Exception
*/
public static String getData(String url, Map<String, String> headerMap) throws Exception {
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach((name, value) -> builder.addHeader(name, value));
}
Call call = okHttpClient.newCall(builder.build());
try {
Response response = call.execute();
String string = response.body().string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* post 请求
*/
public static String postData(String url, String json, Map<String, String> headerMap) throws Exception {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach(builder::addHeader);
}
Request request = builder.post(requestBody).build();
ResponseBody body = okHttpClient.newCall(request).execute().body();
return resolver(body);
}
public static String postUrlencoded(String url, String json, Map<String, String> headerMap) throws Exception {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), json);
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach(builder::addHeader);
}
Request request = builder.post(requestBody).build();
ResponseBody body = okHttpClient.newCall(request).execute().body();
return resolver(body);
}
public static InputStream postDataByPdf(String url, String json, Map<String, String> headerMap) throws Exception {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), json);
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(headerMap)) {
headerMap.forEach(builder::addHeader);
}
Request request = builder.post(requestBody).build();
ResponseBody body = okHttpClient.newCall(request).execute().body();
return body.byteStream();
}
/**
* put 请求
*
* @param url
* @param reqbody
* @param map
* @return
*/
public static String putData(String url, String reqbody, Map<String, String> map) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqbody);
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(map)) {
map.forEach((name, value) -> builder.addHeader(name, value));
}
Request request = builder.put(requestBody).build();
try {
ResponseBody body = okHttpClient.newCall(request).execute().body();
return resolver(body);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* delete 请求
*
* @param url
* @param reqbody
* @param map
* @return
*/
public static String deleteData(String url, String reqbody, Map<String, String> map) {
OkHttpClient okHttpClient = new OkHttpClient();
Request.Builder builder = new Request.Builder().url(url);
if (!CollectionUtils.isEmpty(map)) {
map.forEach((name, value) -> builder.addHeader(name, value));
}
Call call = okHttpClient.newCall(builder.build());
try {
Response response = call.execute();
String string = response.body().string();
return string;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 解析返回值
*
* @param responseBody
* @return
*/
private static String resolver(ResponseBody responseBody) {
InputStream is = null;
String result = null;
try {
is = responseBody.byteStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
String body = null;
StringBuilder sb = new StringBuilder();
while ((body = br.readLine()) != null) {
sb.append(body);
}
is.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
}
标签:body,http,String,url,builder,Request,OkHttp3,发送,return
From: https://www.cnblogs.com/ybbit/p/17703080.html