01-Http请求

1. okHttp

1. maven

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.7.2</version>
</dependency>

2. 同步请求

  • .execute()

3. 异步请求

  • .enqueue()

4. 工具类

@Component
@Slf4j
public class OkHttpUtil {

    /**
     * 发送http请求(异步)
     *
     * @param url   请求地址
     * @param param json字符串
     * @return 响应值
     */
    public String appJsonSync(String url, String param) {
        try {
            log.warn("appJsonSync <-- url: {},param: {}", url, param);
            OkHttpClient okHttpClient = new OkHttpClient();
            okhttp3.RequestBody requestBody = okhttp3.RequestBody
                    .create(param, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            Response execute = okHttpClient.newCall(request).execute();
            ResponseBody body = execute.body();
            return body != null ? body.string() : null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 发送http请求(异步)
     *
     * @param url   请求地址
     * @param param json字符串
     */
    public void appJsonAsync(String url, String param) {
        try {
            log.warn("appJsonAsync <-- url: {}, param: {}", url, param);
            OkHttpClient okHttpClient = new OkHttpClient();
            okhttp3.RequestBody requestBody = okhttp3.RequestBody
                    .create(param, MediaType.parse("application/json; charset=utf-8"));
            Request request = new Request.Builder()
                    .url(url)
                    .post(requestBody)
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    log.warn("call: {}, response: {}", call, Objects.requireNonNull(response.body()).string());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
















 

 
 

 
 























 







 







2. Hutool同步

1. HttpUtil

1. Post, @RequestParam

// url
String url = "http://127.0.0.1:port/ooxx/userLoginAuth";

// 请求参数
HashMap<String, Object> map = new HashMap<>();
map.put("loginName", "user1");
map.put("authKey", "123");

String result = HttpUtil.post(url, map);








 

2. Post, @RequestBody

// url
String url = "http://127.0.0.1:port/ooxx/sync";

// 请求参数
HashMap<String, Object> map = new HashMap<>();
map.put("appId", "131409040112");
map.put("deptI1d", "19910769053");

String result = HttpUtil.post(url, gson.toJson(map));








 

2. HttpRequest

1. header, @RequestParam

// url
String url = "http://127.0.0.1:port/ooxx/userLoginAuth";

// 请求参数
HashMap<String, Object> map = new HashMap<>();
map.put("loginName", "user1");
map.put("authKey", "123");

// 链式构建请求
String result = HttpRequest.post(url)
        .header("X-Ticket", "7e35e53fa44f987f9297ff6b3a8fd568f1")
        .header("ClientTag", "OUTNET_BROWSE")
        .form(map)          // 表单内容
        .timeout(20000)     // 超时,毫秒
        .execute().body();










 
 
 


2. header, @RequestBody

// url
String url = "http://127.0.0.1:port/ooxx/sync";

// 请求参数
HashMap<String, Object> map = new HashMap<>();
map.put("appId", "user1");
map.put("deptId", "123");

// 链式构建请求
String result = HttpRequest.post(url)
        .header("X-Ticket", "7e35e53fa443184c71047204d1330a8f1d")
        .header("ClientTag", "OUTNET_BROWSE")
        .body(gson.toJson(map)) // 表单内容
        .timeout(20000)         // 超时,毫秒
        .execute().body();












 


3. 响应