参考
简介
| <dependencies> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| <scope>runtime</scope> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <optional>true</optional> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.httpcomponents</groupId> |
| <artifactId>httpclient</artifactId> |
| <version>4.5.13</version> |
| </dependency> |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>fastjson</artifactId> |
| <version>1.2.72</version> |
| </dependency> |
| <dependency> |
| <groupId>jakarta.xml.bind</groupId> |
| <artifactId>jakarta.xml.bind-api</artifactId> |
| </dependency> |
| </dependencies> |
| |
| <build> |
| <plugins> |
| |
| <plugin> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-maven-plugin</artifactId> |
| <configuration> |
| <excludes> |
| <exclude> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| </exclude> |
| </excludes> |
| </configuration> |
| </plugin> |
| |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-compiler-plugin</artifactId> |
| <version>3.8.1</version> |
| <configuration> |
| <source>1.8</source> |
| <target>1.8</target> |
| </configuration> |
| </plugin> |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-resources-plugin</artifactId> |
| <version>3.1.0</version> |
| </plugin> |
| <plugin> |
| <groupId>org.apache.maven.plugins</groupId> |
| <artifactId>maven-surefire-plugin</artifactId> |
| <version>2.22.1</version> |
| <configuration> |
| <skipTests>true</skipTests> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
| server: |
| port: 8080 |
| spring: |
| application: |
| name: demo01 |
| logging: |
| level: |
| com: |
| ychen: |
| demo01: debug |
| @SpringBootApplication |
| public class Demo01Application { |
| public static void main(String[] args) { |
| SpringApplication.run(Demo01Application.class, args); |
| } |
| } |
| @GetMapping("/test1") |
| @ResponseBody |
| public String test1(){ |
| |
| CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| |
| HttpGet httpGet = new HttpGet("https://www.baidu.com/"); |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| response = httpClient.execute(httpGet); |
| |
| HttpEntity entity = response.getEntity(); |
| |
| String result = EntityUtils.toString(response.getEntity(), "utf-8"); |
| System.out.println(result); |
| |
| EntityUtils.consume(entity); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| }finally { |
| try { |
| if (response != null){ |
| response.close(); |
| } |
| if (httpGet != null){ |
| httpGet.releaseConnection(); |
| } |
| if (httpClient!= null){ |
| httpClient.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| return "success"; |
| } |
| @GetMapping("/test2") |
| @ResponseBody |
| public String test2(){ |
| |
| CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| |
| HttpGet httpGet = new HttpGet("https://www.baidu.com/"); |
| |
| httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"); |
| |
| httpGet.addHeader("Referer", "https://www.baidu.com/"); |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| response = httpClient.execute(httpGet); |
| |
| HttpEntity entity = response.getEntity(); |
| |
| String result = EntityUtils.toString(response.getEntity(), "utf-8"); |
| System.out.println(result); |
| |
| EntityUtils.consume(entity); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| }finally { |
| try { |
| if (response != null){ |
| response.close(); |
| } |
| if (httpGet != null){ |
| httpGet.releaseConnection(); |
| } |
| if (httpClient!= null){ |
| httpClient.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| return "success"; |
| } |
| @GetMapping("/test3") |
| @ResponseBody |
| public String test3() throws UnsupportedEncodingException { |
| |
| CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
| String passwordParam = "123+abc"; |
| |
| String password = URLEncoder.encode(passwordParam, StandardCharsets.UTF_8.name()); |
| String urlString = "http://localhost:8080/demo?password=" + password; |
| |
| HttpGet httpGet = new HttpGet(urlString); |
| |
| httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"); |
| |
| CloseableHttpResponse response = null; |
| try { |
| |
| response = httpClient.execute(httpGet); |
| |
| HttpEntity entity = response.getEntity(); |
| |
| String result = EntityUtils.toString(response.getEntity(), "utf-8"); |
| System.out.println(result); |
| |
| EntityUtils.consume(entity); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| }finally { |
| try { |
| if (response != null){ |
| response.close(); |
| } |
| if (httpGet != null){ |
| httpGet.releaseConnection(); |
| } |
| if (httpClient!= null){ |
| httpClient.close(); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| return "success"; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)