From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001 From: 田源 <tianyuan@vci-tech.com> Date: 星期四, 03 四月 2025 14:35:02 +0800 Subject: [PATCH] 添加非密字段显示 --- Source/BladeX-Tool/blade-starter-http/src/test/java/org/springblade/core/http/test/HttpRequestDemo.java | 130 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 130 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-http/src/test/java/org/springblade/core/http/test/HttpRequestDemo.java b/Source/BladeX-Tool/blade-starter-http/src/test/java/org/springblade/core/http/test/HttpRequestDemo.java new file mode 100644 index 0000000..1f28e63 --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-http/src/test/java/org/springblade/core/http/test/HttpRequestDemo.java @@ -0,0 +1,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锛孊ASIC锛孒EADERS锛孊ODY锛� 榛樿锛歂ONE + HttpRequest.setGlobalLog(LogLevel.BODY); + + // 鍚屾璇锋眰 url锛屾柟娉曟敮鎸� get銆乸ost銆乸atch銆乸ut銆乨elete + 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銆乤sBytes + // json 绫诲搷搴旓細asJsonNode銆乤sObject銆乤sList銆乤sMap锛岄噰鐢� jackson 澶勭悊 + // xml銆乭tml鍝嶅簲锛歛sDocument锛岄噰鐢ㄧ殑 jsoup 澶勭悊 + // file 鏂囦欢锛歵oFile + + // 鍚屾 + 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); + } + +} -- Gitblit v1.9.3