之前用的RestTemplate,发现一个multipart的http请求始终发不成功,后面就试了下okHttp,发现真的好用,api太清爽了,记录一下使用:

package com.liyao;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class OhHttpTest {

private static final String host = "http://localhost.charlesproxy.com:8080";
// 最好用单例
private static OkHttpClient okHttpClient = new OkHttpClient();

@Test
public void post1() throws IOException {
// RequestBody 有三种构建方式:
// 1. FormBody.Builder,构建application/x-www-form-urlencoded类型的post表单
// 2. MultipartBody.Builder,构建multipart类型的body,多个内容合成到一个body里
// 3. RequestBody.create方法构建body体,传入两个参数:类型和内容
RequestBody body = new FormBody.Builder()
.add("key1", "value1")
.add("key1", "value1&key2=value2")
.addEncoded("key3", "value3?&=") // 这俩方法没看出啥区别,都会urlencode
.build();

Request request = new Request.Builder()
.url(host + "/rest/n/testApi/path")
.post(body)
.build();

Call call = okHttpClient.newCall(request);
call.execute();
}

@Test
public void post2() throws IOException {
RequestBody body = RequestBody.create("{}", MediaType.parse("application/json"));

Request request = new Request.Builder()
.url(host + "/rest/n/testApi/path")
.post(body)
.build();

Call call = okHttpClient.newCall(request);
call.execute();
}

@Test
public void multipart() throws IOException {
InputStream inputStream = new FileInputStream("src/test/resources/mapping.xlsx");
byte[] bs = inputStream.readAllBytes();

// 随便读一个excel文件,当做二进制流处理呗
RequestBody part1 = RequestBody.create(bs, MediaType.parse("application/octet-stream"));

// 通过构造方法指定boundary,一般不需要,只是知道有这么个用法
RequestBody multipart = new MultipartBody.Builder("MyBoundary")
.setType(MultipartBody.MIXED)
.addFormDataPart("m1", "v1")
.addFormDataPart("m2", "v2", part1)
.build();

Request request = new Request.Builder()
.url(host + "/rest/n/testApi/path")
.post(multipart)
.build();

Call call = okHttpClient.newCall(request);
call.execute();
}

@Test
public void get1() {
// get没有body
Request request = new Request.Builder()
.url(host + "/rest/n/testApi/hello")
.get()
.build();

Call call = okHttpClient.newCall(request);

// sync
try {
Response response = call.execute();
// process response
} catch (IOException e) {
e.printStackTrace();
}
}

@Test
public void get2() {
Request request = new Request.Builder()
.url(host + "/rest/n/testApi/hello")
.get()
.build();

Call call = okHttpClient.newCall(request);

// async
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
// process error
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
// process response
}
});

// wait async result
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}