1、HttpClient
maven 依赖
<!---文件上传-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<!-- OKHttp3依赖 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.1</version>
</dependency>
公共
String url = "http://127.0.0.1:8001/";
CloseableHttpClient client = HttpClientBuilder.create().build();
/* Ok httpclient */
private OkHttpClient okclient = new OkHttpClient.Builder().connectTimeout(60,
TimeUnit.SECONDS).build();
测试程序
/* httpclient */
@Test
public void testPut() throws IOException {
HttpPut httpPut = new HttpPut(url + "Put");
HashMap<Object, Object> map = new HashMap<>(1);
map.put("msgBody", "test post request");
httpPut.setHeader("Content-Type", "application/json;charset=utf-8");
CloseableHttpResponse execute = client.execute(httpPut);
System.out.println(EntityUtils.toString(execute.getEntity()));
}
@Test
public void testUpload() throws IOException {
HttpPost httpPost = new HttpPost(url + "upload");
File file = new File("C:\\Users\\Administrator\\Desktop\\AOP.pdf");
FileBody body = new FileBody(file);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addPart("file", body);
HttpEntity build = entityBuilder.build();
httpPost.setEntity(build);
CloseableHttpResponse execute = client.execute(httpPost);
System.out.println(EntityUtils.toString(execute.getEntity()));
}
@Test
public void testDelete() throws IOException {
HttpDelete httpDelete = new HttpDelete(url + "delete");
CloseableHttpResponse execute = client.execute(httpDelete);
System.out.println(EntityUtils.toString(execute.getEntity()));
}
@Test
public void testGet() throws IOException {
HttpGet httpGet = new HttpGet(url + "test");
CloseableHttpResponse execute = client.execute(httpGet);
HttpEntity entity = execute.getEntity();
String s = EntityUtils.toString(entity);
System.out.println(s);
}
@Test
public void testPost() throws IOException {
HttpPost httpPost = new HttpPost(url + "post");
HashMap<Object, Object> map = new HashMap<>(1);
map.put("msgBody", "test post request");
httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
String jsonString = JSONObject.toJSONString(map);
httpPost.setEntity(new StringEntity(jsonString, "UTF-8"));
CloseableHttpResponse execute = client.execute(httpPost);
String string = EntityUtils.toString(execute.getEntity());
System.out.println(string);
}
2、OKHttpClient
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!-- OKHttp3依赖 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.1</version>
</dependency>
测试程序
@Test
public void okClientCancel() throws IOException {
Request request = new Request.Builder()
.url(url + "delete")
.delete()
.build();
Call call = okclient.newCall(request);
Response execute = call.execute();
long cntTime = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() - cntTime > 1000) {
call.cancel();
System.out.println("request cancel");
break;
}
}
System.out.println(execute.body().string());
}
@Test
public void okClientUpload() throws IOException {
MultipartBody file = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "C:\\Users\\Administrator\\Desktop\\AOP.pdf"
, RequestBody.create(MediaType.parse("multipart/form-data"),
new File("C:\\Users\\Administrator\\Desktop\\AOP.pdf")))
.build();
Request request = new Request.Builder()
.url(url + "upload")
.post(file)
.build();
Call call = okclient.newCall(request);
Response execute = call.execute();
System.out.println(execute.body().string());
}
@Test
public void okClientDelete() throws IOException {
Request request = new Request.Builder()
.url(url + "delete")
.delete()
.build();
Call call = okclient.newCall(request);
Response execute = call.execute();
System.out.println(execute.body().string());
}
@Test
public void okClientPost() throws IOException {
HashMap<String, Object> map = new HashMap<>(1);
map.put("msgBody", "v1");
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), JSONObject.toJSONString(map));
Request request = new Request.Builder()
.url(url + "post")
.post(requestBody)
.build();
Call call = okclient.newCall(request);
Response execute = call.execute();
System.out.println(execute.body().string());
}
@Test
public void okClientGet() throws IOException {
Request request = new Request.Builder()
.url(url + "get")
.get()
.build();
Call call = okclient.newCall(request);
Response execute = call.execute();
System.out.println(execute.body().string());
}
3、服务端
@RestController
public class TestGui {
@PostMapping("upload")
public String upload(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
return originalFilename;
}
@PostMapping("post")
public String api(@RequestBody Data msgBody) {
System.out.println("-> " + msgBody.toString());
return msgBody.toString();
}
@RequestMapping("/get")
public String get() {
return "get";
}
@RequestMapping("Put")
public String Put() {
return "Put";
}
@DeleteMapping("delete")
public String delete() {
return "delete";
}
}
class Data {
private String msgBody;
public String getMsgBody() {
return msgBody;
}
public void setMsgBody(String msgBody) {
this.msgBody = msgBody;
}
@Override
public String toString() {
return "msgBody=" + msgBody;
}
}