中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

空間商網(wǎng)站站長之家域名查詢官網(wǎng)

空間商網(wǎng)站,站長之家域名查詢官網(wǎng),wordpress 七牛oss,手機(jī)網(wǎng)頁 模板文章目錄 一、簡(jiǎn)要概述二、封裝過程1. 引入依賴2. 定義腳本執(zhí)行類 三、單元測(cè)試四、其他資源 一、簡(jiǎn)要概述 在Linux中curl是一個(gè)利用URL規(guī)則在命令行下工作的文件傳輸工具,可以說是一款很強(qiáng)大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具&…

文章目錄

  • 一、簡(jiǎn)要概述
  • 二、封裝過程
    • 1. 引入依賴
    • 2. 定義腳本執(zhí)行類
  • 三、單元測(cè)試
  • 四、其他資源

一、簡(jiǎn)要概述

在Linux中curl是一個(gè)利用URL規(guī)則在命令行下工作的文件傳輸工具,可以說是一款很強(qiáng)大的http命令行工具。它支持文件的上傳和下載,是綜合傳輸工具,但按傳統(tǒng),習(xí)慣稱curl為下載工具。它被廣泛應(yīng)用在Unix、多種Linux發(fā)行版中,并且有DOS和Win32、Win64下的移植版本。

借助JAVA的shell腳本執(zhí)行方法,我們可以在curl命令支持下,封裝出一個(gè)代碼精簡(jiǎn)且功能豐富的HTTP調(diào)用工具類。

二、封裝過程

1. 引入依賴

pom.xml

<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.16</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.15.0</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.5.2</version>
</dependency>

2. 定義腳本執(zhí)行類

ShellExecutor.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;import lombok.extern.slf4j.Slf4j;@Slf4j
public class ShellExecutor
{/*** execute命令* * @param command* @throws IOException*/public static void exec(String command){try{log.info("????? WILL EXECUTE COMMAND: {} ?????", command);String[] cmd = SystemUtils.IS_OS_WINDOWS ? new String[] {"cmd", "/c", command} : new String[] {"/bin/sh", "-c", command};Runtime.getRuntime().exec(cmd);}catch (IOException e){log.error(e.getMessage(), e);}}/*** execute命令* * @param command* @return 執(zhí)行結(jié)果* @throws IOException*/public static String execute(String command){try{log.info("????? WILL EXECUTE COMMAND: {} ?????", command);String[] cmd = SystemUtils.IS_OS_WINDOWS ? new String[] {"cmd", "/c", command} : new String[] {"/bin/sh", "-c", command};List<String> resultList = new ArrayList<>();try (BufferedReader br = new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(cmd).getInputStream()))){String line;while ((line = br.readLine()) != null){resultList.add(line);}}return StringUtils.join(resultList, System.lineSeparator());}catch (IOException e){log.error(e.getMessage(), e);return "";}}
}

上面的代碼中,我們我們定義了2個(gè)方法:
exec 適用于不帶返回值或返回值為非String類型的接口調(diào)用。

execute帶String類返回值,適用于restful接口json數(shù)據(jù)返回值的接口調(diào)用。

三、單元測(cè)試


import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;import lombok.extern.slf4j.Slf4j;@Slf4j
public class ShellExecutorTest
{@Testpublic void test001()throws IOException{String response = ShellExecutor.execute("curl https://00fly.online/upload/data.json");log.info(response);}@Testpublic void test002()throws IOException{ShellExecutor.exec("curl -X GET -H  \"Accept:image/jpeg\" -H  \"Content-Type:application/x-www-form-urlencoded\" \"https://00fly.online/upload/2019/02/201902262129360274AKuFZcUfip.jpg\" --output cat.jpg");}@Testpublic void test003()throws IOException{String response = ShellExecutor.execute("curl https://00fly.online/upload/data.json");FileUtils.writeStringToFile(new File("test.json"), response, StandardCharsets.UTF_8, false);}@Testpublic void test004()throws IOException{ShellExecutor.exec("curl https://00fly.online/upload/data.json  -o test.json");ShellExecutor.exec("curl https://00fly.online/upload/data.json  --output test2.json");}@Testpublic void test005()throws IOException{// 模仿瀏覽器、偽造refererString response = ShellExecutor.execute("curl -A \"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.0)\" -e \"blog.csdn.net\" \"https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=lately&noMore=false&username=qq_16127313\"");log.info(response);}@Testpublic void test006()throws IOException{// 偽造refererString response = ShellExecutor.execute("curl -e \"blog.csdn.net\" \"https://blog.csdn.net/community/home-api/v1/get-business-list?page=1&size=20&businessType=lately&noMore=false&username=qq_16127313\"");log.info(response);}
}

四、其他資源

更多CURL命令高級(jí)用法,請(qǐng)參考curl文檔

另外,在線接口文檔knife4j、httpbin等已經(jīng)集成了等價(jià)curl命令,各位可以拷貝測(cè)試自行探索。

注意:如果需要將包含此工具的工程打包成docker鏡像,一定要在鏡像構(gòu)建文件Dockerfile中安裝curl

#安裝curl
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && apk update && apk add curl

在這里插入圖片描述
在這里插入圖片描述


有任何問題和建議,都可以向我提問討論,大家一起進(jìn)步,謝謝!

-over-

http://www.risenshineclean.com/news/4616.html

相關(guān)文章:

  • 做海外網(wǎng)站推廣直播營銷
  • 自己個(gè)人的網(wǎng)站怎么設(shè)計(jì)營銷策劃的六個(gè)步驟
  • 網(wǎng)站使用幫助內(nèi)容銷售策略和營銷策略
  • 網(wǎng)站公司建設(shè)網(wǎng)絡(luò)營銷方法
  • 可以做產(chǎn)品推廣的網(wǎng)站寧波seo關(guān)鍵詞
  • 網(wǎng)站怎么做預(yù)約小程序電商網(wǎng)站seo
  • 個(gè)人博客網(wǎng)站建設(shè)選題說明seo關(guān)鍵詞優(yōu)化案例
  • 荊州建設(shè)局網(wǎng)站軟件外包公司是什么意思
  • 什么網(wǎng)站可以找到防水工程做網(wǎng)絡(luò)推廣的主要工作內(nèi)容
  • 維護(hù)一個(gè)網(wǎng)站需要多少錢怎么制作網(wǎng)頁廣告
  • 網(wǎng)站空間和數(shù)據(jù)庫空間seo基礎(chǔ)入門視頻教程
  • 做黃金理財(cái)?shù)木W(wǎng)站短視頻seo排名
  • 求個(gè)網(wǎng)站好人有好報(bào)百度貼吧怎么做百度網(wǎng)頁推廣
  • bae做網(wǎng)站市場(chǎng)推廣怎么做
  • redis做緩存的網(wǎng)站并發(fā)數(shù)深圳網(wǎng)絡(luò)營銷全網(wǎng)推廣
  • dw做了網(wǎng)站還可以做淘寶詳情嗎西安seo站內(nèi)優(yōu)化
  • 企業(yè)建立網(wǎng)站需要百度廣告投放公司
  • 幫你做海報(bào)網(wǎng)站寧波網(wǎng)絡(luò)建站模板
  • 網(wǎng)站建設(shè)是設(shè)計(jì)師嗎軟件推廣方案經(jīng)典范文
  • 專門做汽車配件的外貿(mào)網(wǎng)站網(wǎng)站目錄
  • 做網(wǎng)站怎么買服務(wù)器嗎關(guān)鍵詞推廣價(jià)格
  • 南京本地網(wǎng)站百度seo自然優(yōu)化
  • 網(wǎng)站建設(shè)方案書內(nèi)容管理制度優(yōu)化seo教程技術(shù)
  • css修改Wordpressseo是搜索引擎嗎
  • 在ps中做網(wǎng)站首頁的尺寸品牌營銷策劃方案
  • 17做網(wǎng)店網(wǎng)站池尾百度付費(fèi)問答平臺(tái)
  • wordpress轉(zhuǎn)移服務(wù)器免費(fèi)seo優(yōu)化工具
  • 網(wǎng)站建設(shè)方案書 個(gè)人成品網(wǎng)站源碼
  • 我想在阿里巴巴網(wǎng)站開店_怎么做站長工具域名查詢ip
  • 網(wǎng)站手機(jī)頁面如何做超級(jí)外鏈