java如何發(fā)送get請求獲取數(shù)據(jù)(附代碼)
1、使用Java標準庫中的HttpURLConnection:
代碼示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class GetRequestUsingHttpURLConnection {
public static void main(String[] args) {
String url = "https://api.example.com/data"; // 替換成實際的API地址
try {
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();
System.out.println(response.toString());
} else {
System.out.println("GET request failed. Response code: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、使用OkHttp庫:
安裝依賴
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
代碼示例
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class GetRequestUsingOkHttp {
public static void main(String[] args) {
String url = "https://api.example.com/data"; // 替換成實際的API地址
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.get()
.build();
try {
Response response = httpClient.newCall(request).execute();
if (response.isSuccessful()) {
String responseData = response.body().string();
System.out.println(responseData);
} else {
System.out.println("GET request failed. Response code: " + response.code());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}附:java發(fā)送get請求傳json數(shù)據(jù)
在Java中發(fā)送GET請求傳遞JSON數(shù)據(jù),可以使用HttpClient庫來實現(xiàn)。以下是一個示例代碼:
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGetWithEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class Main {
public static void main(String\[\] args) {
HttpClient httpClient = HttpClientBuilder.create().build();
String url = "http://example.com/api";
String json = "{\"key\":\"value\"}";
try {
HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
httpGet.setEntity(new StringEntity(json));
HttpResponse response = httpClient.execute(httpGet);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} catch (IOException e) {
e.printStackTrace();
}
}
}
在這個示例中,我們使用HttpClient庫創(chuàng)建了一個HttpClient對象,并指定了請求的URL和JSON數(shù)據(jù)。然后,我們創(chuàng)建了一個HttpGetWithEntity對象,并將JSON數(shù)據(jù)設置為請求的實體。最后,我們執(zhí)行GET請求并獲取響應的內(nèi)容。
請注意,這只是一個示例代碼,你需要根據(jù)你的實際情況進行適當?shù)男薷摹?/p>
總結(jié)
到此這篇關于java如何發(fā)送get請求獲取數(shù)據(jù)的文章就介紹到這了,更多相關java發(fā)送get請求獲取數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud Stream微服務消息框架原理及實例解析
這篇文章主要介紹了Spring Cloud Stream微服務消息框架原理及實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06
SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解
這篇文章主要介紹了SpringBoot之通過BeanPostProcessor動態(tài)注入ID生成器案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09
springboot使用校驗框架validation校驗的示例
這篇文章主要介紹了springboot使用校驗框架validation校驗的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
SpringBoot+JUnit5+MockMvc+Mockito單元測試的實現(xiàn)
今天聊聊如何在 SpringBoot 中集成 Junit5、MockMvc、Mocktio。Junit5 是在 Java 棧中應用最廣的測試框架,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
簡單了解Spring中BeanFactory與FactoryBean的區(qū)別
這篇文章主要介紹了簡單了解Spring中BeanFactory與FactoryBean的區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12

