¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * 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); |
| | | } |
| | | |
| | | } |