网络请求工具类
基于springboot
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
代码如下
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
-
网络请求工具类
*/
public class OkHttpUtil {private static final String MEDIA_TYPE = “application/x-www-form-urlencoded; charset=utf-8”;
private static final String METHOD_GET = “GET”;
private static final String METHOD_POST = “POST”;
private static final String METHOD_PUT = “PUT”;
private static final String METHOD_DELETE = “DELETE”;
private static OkHttpClient okHttpClient;private OkHttpUtil() {}
static{
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(10000, TimeUnit.MILLISECONDS);
builder.writeTimeout(10000, TimeUnit.MILLISECONDS);
builder.connectTimeout(10000, TimeUnit.MILLISECONDS);
okHttpClient = builder.build();
}
/**- 异步post方法
- @param url
- @param baseRequest
- @param callback
*/
public static void enqueuePost(String url, String content, Callback callback) {
Request request = createRequest(url, METHOD_POST, content);
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
- 异步get方法
- @param url
- @param callback
*/
public static void enqueueGet(String url, Callback callback) {
Request request = createRequest(url, METHOD_GET, null);
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
- 同步post方法
- 注意此方法不能在主线程中调用
- @param url
*/
public static Response post(String url, String content) throws IOException {
Request request = createRequest(url, METHOD_POST, content);
Call call = okHttpClient.newCall(request);
Response response = call.execute();
return response;
}
/**
- 同步get方法
- 注意此方法不能在主线程中调用
- @param url
*/
public static Response get(String url) throws IOException {
Request request = createRequest(url, METHOD_GET, null);
Call call = okHttpClient.newCall(request);
Response response = call.execute();
return response;
}
/**
-
创建request对象
-
@param url
-
@param method
-
@param baseRequest 项目中自己的的baseRequest对象
-
@return
*/
private static Request createRequest(String url, String method, String content) {
RequestBody requestBody = null;
if (content != null) {
MediaType JSON = MediaType.parse(MEDIA_TYPE);
requestBody = RequestBody.create(JSON, content);}
Request.Builder builder = new Request.Builder().url(url);
Request request = null;
switch (method) {
case METHOD_GET:
request = builder.build();
break;
case METHOD_POST:
request = builder.post(requestBody).build();
break;
case METHOD_PUT:
if (requestBody != null) {
request = builder.put(requestBody).build();
}
break;
case METHOD_DELETE:
if (requestBody != null) {
request = builder.delete(requestBody).build();
} else {
request = builder.delete().build();
}
break;
}
return request;
}
//测试使用
public static void main(String[] args)throws Exception {
Response response = post(“http://10.10.1.85:3000/mock/18/account/logout.html”, “clientId=e”);
System.out.println(response.body().string());
response = post(“http://10.10.1.85:3000/mock/18/account/login.html”, “brokerId=27&deptId=227&accountType=8&loginId=223100001178&loginPWD=991122&tradeId=223100001178&tradePWD=991122”);
System.out.println(response.body().string());
}
}