xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *      Copyright (c) 2018-2028, DreamLu All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: DreamLu 卢春梦 (596392912@qq.com)
 */
package org.springblade.core.http.test;
 
import com.fasterxml.jackson.databind.JsonNode;
import org.springblade.core.http.HttpRequest;
import org.springblade.core.http.LogLevel;
import org.springblade.core.http.ResponseSpec;
import okhttp3.Cookie;
import org.springblade.core.tool.utils.Base64Util;
 
import java.net.URI;
import java.time.Duration;
import java.util.Optional;
 
/**
 * This example of blade http
 *
 * @author L.cm
 */
public class HttpRequestDemo {
 
    public void doc() {
        // 设定全局日志级别 NONE,BASIC,HEADERS,BODY, 默认:NONE
        HttpRequest.setGlobalLog(LogLevel.BODY);
 
        // 同步请求 url,方法支持 get、post、patch、put、delete
        HttpRequest.get("https://www.baidu.com")
            .log(LogLevel.BASIC)             //设定本次的日志级别,优先于全局
            .addHeader("x-account-id", "blade001") // 添加 header
            .addCookie(new Cookie.Builder()  // 添加 cookie
                .name("sid")
                .value("blade_user_001")
                .build()
            )
            .query("q", "blade") //设置 url 参数,默认进行 url encode
            .queryEncoded("name", "encodedValue")
            .formBuilder()    // 表单构造器,同类 multipartFormBuilder 文件上传表单
            .add("id", 123123) // 表单参数
            .execute()// 发起请求
            .asJsonNode();
        // 结果集转换,注:如果网络异常等会直接抛出异常。
        // 同类的方法有 asString、asBytes
        // json 类响应:asJsonNode、asObject、asList、asMap,采用 jackson 处理
        // xml、html响应:asDocument,采用的 jsoup 处理
        // file 文件:toFile
 
        // 同步
        String html = HttpRequest.post("https://www.baidu.com")
            .execute()
            .onSuccess(ResponseSpec::asString);// 处理响应,有网络异常等直接返回 null
 
        // 同步
        String text = HttpRequest.patch("https://www.baidu.com")
            .execute()
            .onSuccess(ResponseSpec::asString);
        // onSuccess http code in [200..300) 处理响应,有网络异常等直接返回 null
 
        // 发送异步请求
        HttpRequest.delete("https://www.baidu.com")
            .async() // 开启异步
            .onFailed((request, e) -> {    // 异常时的处理
                e.printStackTrace();
            })
            .onSuccessful(responseSpec -> { // 消费响应成功 http code in [200..300)
                // 注意:响应结果流只能读一次
                JsonNode jsonNode = responseSpec.asJsonNode();
            });
    }
 
    public static void main(String[] args) {
        // 设定全局日志级别
        HttpRequest.setGlobalLog(LogLevel.BODY);
 
        // 同步,异常时 返回 null
        String html = HttpRequest.get("https://www.baidu.com")
            .connectTimeout(Duration.ofSeconds(1000))
            .query("test", "a")
            .query("name", "張三")
            .query("x", 1)
            .query("abd", Base64Util.encode("123&$#%"))
            .queryEncoded("abc", Base64Util.encode("123&$#%"))
            .execute()
            .onFailed(((request, e) -> {
                e.printStackTrace();
            }))
            .onSuccess(ResponseSpec::asString);
        System.out.println(html);
 
        // 同步调用,返回 Optional,异常时返回 Optional.empty()
        Optional<String> opt = HttpRequest.post(URI.create("https://www.baidu.com"))
            .bodyString("Important stuff")
            .formBuilder()
            .add("a", "b")
            .execute()
            .onSuccessOpt(ResponseSpec::asString);
 
        // 同步,成功时消费(处理) response
        HttpRequest.post("https://www.baidu.com/some-form")
            .addHeader("X-Custom-header", "stuff")
            .execute()
            .onFailed((request, e) -> {
                e.printStackTrace();
            })
            .onSuccessful(ResponseSpec::asString);
 
        // async,异步执行结果,失败时打印堆栈
        HttpRequest.get("https://www.baidu.com/some-form")
            .async()
            .onFailed((request, e) -> {
                e.printStackTrace();
            })
            .onSuccessful(System.out::println);
    }
 
}