该工具类都是同步请求简单的封装了下OkHttp
maven依赖
<!--fastjson 依赖添加-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!--log4j 依赖添加-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--slf4j 依赖添加-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<!--okhttp3 依赖添加-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
<!--lombok 依赖添加-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
工具类
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okio.BufferedSink;
import okio.Okio;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class OkHttpUtil {
/**
* Get请求
* @param url URL地址
* @return 返回结果
*/
public static String get(String url){
String result=null;
try {
OkHttpClient okHttpClient=new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
result=response.body().string();
log.info("Get请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Get]请求异常",e);
return result;
}
}
/**
* Post请求
* @param url URL地址
* @param params 参数
* @return 返回结果
*/
public static String post(String url,Map<String,String> params){
String result=null;
if (params==null){
params=new HashMap<String, String>();
}
try {
OkHttpClient okHttpClient=new OkHttpClient();
FormBody.Builder formBodyBuilder = new FormBody.Builder();
//添加参数
log.info("params:{}", JSON.toJSONString(params));
for (Map.Entry<String,String> map:params.entrySet()){
String key=map.getKey();
String value;
if (map.getValue()==null){
value="";
}else{
value=map.getValue();
}
formBodyBuilder.add(key,value);
}
FormBody formBody =formBodyBuilder.build();
Request request = new Request.Builder().url(url).post(formBody).build();
Response response = okHttpClient.newCall(request).execute();
result=response.body().string();
log.info("Post请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Post]请求异常",e);
return result;
}
}
/**
* 上传文件请求(Post请求)
* @param url URL地址
* @param params 文件 key:参数名 value:文件 (可以多文件)
* @return 返回结果
*/
public static String upload(String url, Map<String, File> params){
String result=null;
try {
OkHttpClient okHttpClient=new OkHttpClient();
MultipartBody.Builder multipartBodyBuilder =new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Map.Entry<String,File> map:params.entrySet()){
String key=map.getKey();
File file=map.getValue();
if (file==null||(file.exists() && file.length() == 0)){
continue;
}
multipartBodyBuilder.addFormDataPart(key,file.getName(),RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
RequestBody requestBody =multipartBodyBuilder.build();
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response = okHttpClient.newCall(request).execute();
result=response.body().string();
log.info("Upload请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Upload]请求异常",e);
return result;
}
}
/**
* 下载文件请求(Get请求)
* @param url URL地址
* @param savePath 保存路径(包括文件名)
* @return 文件保存路径
*/
public static String download(String url,String savePath){
String result=null;
try {
OkHttpClient okHttpClient=new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
File file=new File(savePath);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
BufferedSink sink =Okio.buffer((Okio.sink(file)));
sink.writeAll(response.body().source());
sink.flush();
sink.close();
result=savePath;
log.info("Download请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Download]请求异常",e);
return result;
}
}
}