OkHttp是一个处理网络请求的开源项目,是Android端一个较新的轻量级网络框架,支持HTTP/HTTPS协议、请求头设置、响应解析等功能,并且支持异步调用,因此在接口测试中也是一种非常优秀的选择。
OkHttp是一个高效的HTTP客户端,其特性包括:
优点
缺点
官网地址:https://square.github.io/okhttp/
GitHub地址:https://github.com/square/okhttp
接口文档地址:https://square.github.io/okhttp/3.x/okhttp/
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
public static void getTest() throws IOException {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder() //默认是GET请求
.url("http://localhost:4568/getdemo")
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println("响应体结果为:"+result);
}
response.close();
}
private static void getWithParam() throws IOException {
String URL="http://localhost:4568";
OkHttpClient client=new OkHttpClient();
HttpUrl.Builder httpUrl=HttpUrl.parse(String.format("%s/getwithparam",URL))
.newBuilder()
.addQueryParameter("name","tom")
.addQueryParameter("age","10");
Request request=new Request.Builder()
.url(httpUrl.build())
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println(result);
}
response.close();
}
public static void postWithForm() throws IOException {
OkHttpClient client=new OkHttpClient();
RequestBody requestBody=new FormBody.Builder()
.add("name","tom")
.add("age","10")
.build();
Request request=new Request.Builder()
.url("http://localhost:4568/post/form/demo")
.post(requestBody)
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println(result);
}
response.close();
}
public static void postWithJson() throws IOException {
OkHttpClient client=new OkHttpClient();
String data="{\"name\": \"tom\", \"age\": \"10\"}";
RequestBody requestBody=RequestBody.create(data,MediaType.parse("application/json"));
Request request=new Request.Builder()
.url("http://localhost:4568/post/json/demo")
.post(requestBody)
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println("响应结果为:"+result);
}
response.close();
}
public static void postWihtFile() throws IOException {
OkHttpClient client=new OkHttpClient();
MediaType mediaType=MediaType.parse("image/png");
File file=new File("\\data\\image\\test.png");
RequestBody requestBody=new MultipartBody.Builder().setType(MultipartBody.FORM)
// .addFormDataPart("参数名","文件名",RequestBody.create(文件地址,文件格式))
.addFormDataPart("fileData","test",RequestBody.create(file,mediaType))
.build();
Request request=new Request.Builder()
.url("http://localhost:8888/uploadfile")
.post(requestBody)
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println(result);
}
response.close();
}
public static void put() throws IOException {
OkHttpClient client=new OkHttpClient();
String URL="http://localhost:4568/put/demo";
RequestBody requestBody=new FormBody.Builder()
.add("name","tom")
.add("age","10")
.build();
Request request=new Request.Builder()
.url(URL)
.put(requestBody)
.build();
Response response=client.newCall(request).execute();
if (response.isSuccessful()){
String result=response.body().string();
System.out.println(result);
}
response.close();
}
public static void delete() throws IOException {
OkHttpClient client=new OkHttpClient();
String param="1112";
String URL = String.format("http://localhost:4568/delete/demo/%s",param);
Request request=new Request.Builder()
.delete()
.url(URL)
.build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
String result=response.body().string();
System.out.println(result);
}
response.close();
}