java后臺發(fā)起get請求獲取響應(yīng)數(shù)據(jù)
更新時(shí)間:2019年08月30日 14:48:27 作者:基隆
這篇文章主要為大家詳細(xì)介紹了java后臺發(fā)起get請求獲取響應(yīng)數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java后臺發(fā)起get請求獲取響應(yīng)數(shù)據(jù),供大家參考,具體內(nèi)容如下
學(xué)習(xí)記錄:
話不多說直接上代碼:
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請求測試
* @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請求
* @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é)果如圖:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot框架集成ElasticSearch實(shí)現(xiàn)過程示例詳解
這篇文章主要為大家介紹了SpringBoot如何集成ElasticSearch的實(shí)現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
一篇文章了解Jackson注解@JsonFormat及失效解決辦法
這篇文章主要給大家介紹了關(guān)于如何通過一篇文章了解Jackson注解@JsonFormat及失效解決辦法的相關(guān)資料,@JsonFormat注解是一個(gè)時(shí)間格式化注解,用于格式化時(shí)間,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
spring boot整合redis實(shí)現(xiàn)RedisTemplate三分鐘快速入門
這篇文章主要介紹了spring boot整合redis實(shí)現(xiàn)RedisTemplate三分鐘快速入門,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
如何實(shí)現(xiàn)自定義SpringBoot的Starter組件
這篇文章主要介紹了實(shí)現(xiàn)自定義SpringBoot的Starter組件的示例代碼,想要自定義starter組件,首先要了解springboot是如何加載starter的,也就是springboot的自動裝配機(jī)制原理,本文結(jié)合示例代碼詳細(xì)講解,需要的朋友可以參考下2023-02-02

