使用okhttp進行介面測試
阿新 • • 發佈:2019-01-28
package com.woood.okhttp;
import okhttp3.*;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
public class Test {
OkHttpClient client = new OkHttpClient();
/**
* post使用的json頭
*/
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8" );
/**
* get介面操作方法 *
*
* @param url 介面地址
* @return
* @throws IOException
*/
public String get(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("GET");
return response.body().string();
} else {
throw new IOException("錯誤碼: " + response);
}
}
/**
* 使用鍵值對呼叫post方法
* @param url 介面連結
* @param map 鍵值對
* @return
* @throws IOException
*/
private String post(String url, Map<String, String> map) throws IOException {
FormBody.Builder build = new FormBody.Builder();
for (Map.Entry<String, String> item : map.entrySet()) {
build.add(item.getKey(), item.getValue());
}
RequestBody formBody = build.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("POST-Key-Value");
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
/**
* post方法,使用json
*
* @param url 介面連結
* @param json 請求體
* @return
* @throws IOException
*/
private String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("POST-JSON");
return response.body().string();
} else {
throw new IOException("錯誤碼:" + response);
}
}
/**
* put方法,使用map傳遞鍵值對
* @param url 介面連結
* @param map 鍵值對
* @return
* @throws IOException
*/
private String put(String url, Map<String, String> map) throws IOException {
FormBody.Builder build = new FormBody.Builder();
for (Map.Entry<String, String> item : map.entrySet()) {
build.add(item.getKey(), item.getValue());
}
RequestBody formBody = build.build();
Request request = new Request.Builder()
.url(url)
.put(formBody)
.build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("PUT-Key-Value");
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
/**
* put方法,使用json
* @param url 介面連結
* @param json 鍵值對
* @return
* @throws IOException
*/
private String put(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).put(body).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println("PUT-JSON");
return response.body().string();
} else {
throw new IOException("錯誤碼:" + response);
}
}
/**
* 測試方法
*
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
String string = null;
// try {
// string = test.get("http://172.21.3.232:8088/operationLog");
// System.out.println(string);
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// try {
// string = test.post("http://172.21.3.232:8088/operationLog?");
// System.out.println(string);
// } catch (IOException e) {
// e.printStackTrace();
// }
try {
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("operName", "android");
map.put("loginName", "bug");
map.put("ipAddress", "XXXXXXXXXXXXXXX");
map.put("result", "sucess");
map.put("description", "描述");
map.put("failureReason", "失敗原因");
map.put("moduleId", "1");
string = test.post("http://172.21.3.232:8088/operationLog?", map);
//string = test.post("http://172.21.3.232:8088/operatorRole/addOperatorRole","{\"roleName\":\"aa\",\"roleDesc\":\"bb\",\"id\":\"\"}");
System.out.println(string);
} catch (IOException e) {
e.printStackTrace();
}
// try {
// string = test.put("http://172.21.3.232:8088/operatorRole/1","{\"id\": 1,\"roleDesc\": \"string\",\"roleName\": \"string\"}");
// System.out.println(string);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}