OkHttp的基本使用
package com.dgy;
import okhttp3.*;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
public class TestRequest {
private static final String BASE_URL = "http://localhost:8080";
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
@Test
public void test1() throws IOException {
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/test2").newBuilder();
RequestBody requestBody = RequestBody.create(JSON, "");
Request request = new Request.Builder().url(urlBuilder.build()).post(requestBody).build();
Response execute = new OkHttpClient().newCall(request).execute();
System.out.println(execute.body().string());
}
@Test
public void test2() throws IOException {
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/test3").newBuilder()
.addQueryParameter("date", "2018/1/1");
Request request = new Request.Builder()
.url(urlBuilder.build())
.get()
.header("token", "hello")
.build();
Response response = new OkHttpClient().newCall(request).execute();
System.out.println(response.body().string());
System.out.println(response.header("Content-Type"));
System.out.println(response.header("Date"));
System.out.println(response.header("Connection"));
}
@Test
public void test3() throws IOException {
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/test4").newBuilder()
.addQueryParameter("number", "8552");
Student student1 = new Student();
student1.setAge(58);
student1.setName("hello");
Request request = new Request.Builder()
.post(FormBody.create(JSON, com.alibaba.fastjson.JSON.toJSONString(student1)))
.url(urlBuilder.build())
.build();
Response response = new OkHttpClient().newCall(request).execute();
System.out.println(response.body().string());
}
@Test
public void test5() throws IOException {
HttpUrl.Builder urlBuiler = HttpUrl.parse(BASE_URL + "/test5")
.newBuilder()
.addQueryParameter("name", "dgy")
.addQueryParameter("age", "25");
Student student = new Student();
student.setName("stu");
student.setAge(12);
List<Integer> list = new ArrayList<>();
list.add(5);
list.add(6);
Map<String, Object> map = new HashMap<>();
map.put("list", list);
map.put("stu", student);
System.out.println(com.alibaba.fastjson.JSON.toJSONString(map));
Request request = new Request.Builder()
.header("token", "tokendfghjkl;")
.post(RequestBody.create(JSON, com.alibaba.fastjson.JSON.toJSONString(map)))
.url(urlBuiler.build())
.build();
Response response = new OkHttpClient().newCall(request).execute();
System.out.println(response.body().string());
}
@Test
public void test6() {
HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/test3")
.newBuilder()
.addQueryParameter("date", "2020-1-1");
Request request = new Request.Builder()
.url(urlBuilder.build())
.get()
.header("token", "mytest3token")
.build();
Call call = new OkHttpClient().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("请求失败了");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("请求成功" + response.body().string());
}
});
}
@Test
public void test7() throws IOException {
HttpUrl httpUrl = HttpUrl.parse(BASE_URL + "/test6")
.newBuilder().build();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", "22755")
.addFormDataPart("pwd", "dgyu177")
.build();
RequestBody body1 = new FormBody.Builder()
.add("id", "22755")
.add("pwd", "djlkjll")
.build();
Request request = new Request.Builder()
.url(httpUrl)
.post(body1)
.build();
Response response = new OkHttpClient().newCall(request).execute();
System.out.println(response.body().string());
}
@Test
public void test8() throws IOException {
HttpUrl httpUrl = HttpUrl.parse(BASE_URL + "/test7")
.newBuilder().build();
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "jjj.png",
RequestBody.create(MediaType.parse("application/octet-stream"),
new File("F:\\图片\\1920x1080_px_Dark_Ens_fish_minimalism_red-1299857.jpg")))
.build();
Request request = new Request.Builder()
.url(httpUrl)
.post(body)
.build();
Response response = new OkHttpClient().newCall(request).execute();
System.out.println(response);
System.out.println(response.body().string());
}
}