[Java] 纯文本查看 复制代码 public class OkhttpUtil {
public static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient();
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
public static final MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/x-markdown; charset=utf-8");
static {
//cookie状态保持
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
OK_HTTP_CLIENT.setCookieHandler(cookieManager);
}
public static ResponseBody syncGet(String url) {
Request request = new Request.Builder().url(url).build();
return getResponseBody(request);
}
public static ResponseBody syncGet(String url, Headers headers) {
Request request = null;
if (headers != null) {
request = new Request.Builder().url(url).headers(headers).build();
} else {
request = new Request.Builder().url(url).build();
}
return getResponseBody(request);
}
public static ResponseBody syncGet(String url, Headers headers, Map<Object, Object> params) {
Request.Builder builder = new Request.Builder();
StringBuilder sb = new StringBuilder(url);
judgeParams(params, sb);
if (headers != null) {
builder = builder.url(sb.toString()).headers(headers);
} else {
builder = builder.url(sb.toString());
}
Request request = builder.build();
return getResponseBody(request);
}
public static void asynGet(String url, Headers headers, Map<Object, Object> params, Callback callback) {
Request.Builder builder = new Request.Builder();
StringBuilder sb = new StringBuilder(url);
judgeParams(params, sb);
if (headers != null) {
builder = builder.url(sb.toString()).headers(headers);
} else {
builder = builder.url(sb.toString());
}
Request request = builder.build();
OK_HTTP_CLIENT.newCall(request).enqueue(callback);
}
private static void judgeParams(Map<Object, Object> params, StringBuilder sb) {
if (params != null) {
int params_size = params.size();
if (params_size > 0) {
int index = 0;
sb.append("?");
for (Map.Entry entry : params.entrySet()) {
index++;
if (index != params_size) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
} else {
sb.append(entry.getKey()).append("=").append(entry.getValue());
}
}
}
}
}
private static ResponseBody getResponseBody(Request request) {
try {
Response response = OK_HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful()) {
return response.body();
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return null;
}
public static ResponseBody syncPostForm(String url, Headers headers, Map<String, String> params) {
Request.Builder builder = new Request.Builder();
if (params != null && params.size() > 0) {
FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
formEncodingBuilder.add(entry.getKey(), entry.getValue());
}
RequestBody requestBody = formEncodingBuilder.build();
builder.post(requestBody);
}
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
public static ResponseBody syncPostByJson(String url, Headers headers, String json) {
Request.Builder builder = new Request.Builder();
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, json);
builder.post(requestBody);
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
public static ResponseBody syncPostByString(String url, Headers headers, String str) {
Request.Builder builder = new Request.Builder();
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, str);
builder.post(requestBody);
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
public static ResponseBody syncPostByStream(String url, Headers headers, InputStream is) throws IOException {
Request.Builder builder = new Request.Builder();
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, ByteStreams.toByteArray(is));
builder.post(requestBody);
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
public static ResponseBody syncPostByFile(String url, Headers headers, File file) {
Request.Builder builder = new Request.Builder();
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_TEXT, file);
builder.post(requestBody);
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
public static ResponseBody syncPost(String url, RequestBody requestBody, Headers headers) {
Request.Builder builder = new Request.Builder();
builder.post(requestBody);
if (headers != null) {
builder = builder.url(url).headers(headers);
} else {
builder = builder.url(url);
}
Request request = builder.build();
return getResponseBody(request);
}
} |