java后臺(tái)發(fā)起get請(qǐng)求獲取響應(yīng)數(shù)據(jù)
本文實(shí)例為大家分享了java后臺(tái)發(fā)起get請(qǐng)求獲取響應(yīng)數(shù)據(jù),供大家參考,具體內(nèi)容如下
學(xué)習(xí)記錄:
話不多說(shuō)直接上代碼:
package com.jl.chromeTest; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; /** * get請(qǐng)求測(cè)試 * @author liujilong * @since 2019-7-18 10:26:49 */ public class Test { @org.junit.Test public void test() throws Exception{ String result = get("http://www.baidu.com"); System.out.println("result====="+result); } /** * get請(qǐng)求 * @param url * @return * @throws Exception */ public String get(String url) throws Exception { String content = null; URLConnection urlConnection = new URL(url).openConnection(); HttpURLConnection connection = (HttpURLConnection) urlConnection; connection.setRequestMethod("GET"); //連接 connection.connect(); //得到響應(yīng)碼 int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader (connection.getInputStream(), StandardCharsets.UTF_8)); StringBuilder bs = new StringBuilder(); String l; while ((l = bufferedReader.readLine()) != null) { bs.append(l).append("\n"); } content = bs.toString(); } return content; } }
結(jié)果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot框架集成ElasticSearch實(shí)現(xiàn)過(guò)程示例詳解
這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實(shí)現(xiàn)過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Spring?Aop常見(jiàn)注解與執(zhí)行順序詳解
這篇文章主要給大家介紹了關(guān)于Spring?Aop常見(jiàn)注解與執(zhí)行順序的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02一篇文章了解Jackson注解@JsonFormat及失效解決辦法
這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章了解Jackson注解@JsonFormat及失效解決辦法的相關(guān)資料,@JsonFormat注解是一個(gè)時(shí)間格式化注解,用于格式化時(shí)間,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11spring boot整合redis實(shí)現(xiàn)RedisTemplate三分鐘快速入門(mén)
這篇文章主要介紹了spring boot整合redis實(shí)現(xiàn)RedisTemplate三分鐘快速入門(mén),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12如何實(shí)現(xiàn)自定義SpringBoot的Starter組件
這篇文章主要介紹了實(shí)現(xiàn)自定義SpringBoot的Starter組件的示例代碼,想要自定義starter組件,首先要了解springboot是如何加載starter的,也就是springboot的自動(dòng)裝配機(jī)制原理,本文結(jié)合示例代碼詳細(xì)講解,需要的朋友可以參考下2023-02-02