package com.yzw.apitest.service;
import com.alibaba.fastjson.JSONObject;
import com.yzw.apitest.utils.ParamUtil;
import okhttp3.*;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.HashMap;
@Component
public class OkHttpService {
private OkHttpClient okHttpClient;
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public OkHttpService() {
okHttpClient = new OkHttpClient();
}
public String doGet(String host, String api, HashMap<String,String> paramMap) throws IOException {
String url = host + api;
String result = "";
Request request;
if (paramMap.size()>0) {
try {
url = host + api + ParamUtil.urlString(paramMap);
}catch (Exception e){
e.printStackTrace();
result = "参数格式不正确" + e.getMessage();
}
}
System.out.println("url is :" + url);
request = new Request.Builder()
.url(url)
.build();
try {
result = okHttpClient.newCall(request).execute().body().string();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage();
}
return result;
}
public String doPost(String host, String api, HashMap<String, Object> params, String headers) throws IOException {
String url = "";
String result = "";
Request request;
RequestBody requestBody;
url = host + api;
JSONObject json = new JSONObject(params);
//常规的参数请求
if (headers == null || "".equals(headers)) {
FormBody.Builder temp = new FormBody.Builder();
for (String key : params.keySet()){
temp.add(key, (String) params.get(key));
}
requestBody = temp.build();
request = new Request.Builder().url(url).post(requestBody).build();
}
//requestbody请求
else if (headers.contains("application/json")){
requestBody = RequestBody.create(JSON, json.toJSONString());
request = new Request.Builder().
addHeader("Content-Type", "application/json;charset=UTF-8")
.url(url)
.post(requestBody)
.build();
}else {
return "暂未处理的header类型";
}
try {
result = okHttpClient.newCall(request).execute().body().string();
} catch (IOException e) {
e.printStackTrace();
result = e.getMessage();
}
return result;
}
// public String doHttpRequest(String host, TestCases testCases){
// String method = testCases.getMethod();
// OkHttpUtil okHttpUtil = new OkHttpUtil();
// String result;
// switch (method){
// case "GET": {
// try {
// result = okHttpUtil.doGet(host, testCases.getApi(), testCases.getParams());
// } catch (IOException e) {
// e.printStackTrace();
// result = e.getMessage();
// };
// break;
// }
// case "POST": {
// Map<String, Object> postParamMap = null;
// try {
// postParamMap = ParamsUtil.toMap(testCases.getParams());
// } catch (Exception e) {
// result = e.getMessage();
// e.printStackTrace();
// }
// try {
// result = okHttpUtil.doPost(host, testCases.getApi(), postParamMap, testCases.getHeaders());
// } catch (IOException e) {
// e.printStackTrace();
// result = e.getMessage();
// } ;
// break;
// }
// default:
// result = "test_cases表的method类型暂不支持";
// }
// return result;
// }
public static void main(String[] args) throws IOException {
// OkHttpUtil okHttpUtil = new OkHttpUtil();
// String s = okHttpUtil.doGet("http://localhost:8090", "/user/getUser/1", "");
// System.out.println("==================" + s);
// OkHttpUtil okHttpUtil1 = new OkHttpUtil();
//
// Map<String, Object> param = new HashMap<String, Object>();
// param.put("id", "1");
// param.put("name", "okhttp");
// param.put("code", "234234");
// String s1 = okHttpUtil1.doPost("http://localhost:8090", "/service/update", param, "");
// System.out.println(s1);
File file = new File("PostTestCaseTemplate.java");
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
System.out.println(bufferedReader.readLine());
}
}
白话Learning
- 粉丝: 4733
- 资源: 3098
最新资源
- 脉振方波高频注入仿真程序,永磁同步电机高频方波注入 1,仿真为离散模型,开关频率5k,注入频率取开关频率的一半是2.5k 2,程序在1.5s加载,在4.1s不再注入方波,从波形可以看到低速区可以带
- 自动驾驶道路天气分类数据集,含标签
- 信息安全题目i已完全和
- 变压器磁场,固体力学,声学耦合有限元仿真模型 磁致伸缩效应,铁心变形位移量,声学频域分析等,COMSOL有限元仿真模型,模型真实可靠 优化设计,故障诊断
- tombstones--SN.--2025-0102-1457.tar.gz
- 视频编码标准VVC中帧内编码复杂度降低的机会与方法
- COMSOL有限元仿真模型,三相变压器电磁-声-结构力多物理耦合模型,应力分析,磁密分析,声场分析
- 电源PFC入门 TI单相三相维也纳VIENNA整流器无桥 原理图PCB中文资料
- 基于S7-200 PLC和MCGS组态的灌装贴标生产线系统 带解释的梯形图程序,接线图原理图图纸,io分配,组态画面
- Matlab 电力系统各种故障波形仿真,单相接地故障,两相间短路,两相接地短路,三相短路
- 操作系统题目啊实打实大苏打撒
- JAVA期末大作业基于javaweb的医药管理系统设计源码+数据库.zip
- 基于CNN卷积神经网络的网络入侵检测python源码+全部数据+文档(高分毕业设计)
- python语言编程实现燃放烟花效果的代码
- 2-远程协助工具 Veyon 4.9.0
- 文字生成视频-动漫-pix
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈