目录
- 1、使用Java标准库中的HttpURLConnection:
- 2、使用OkHttp库:
- 附:java发送get请求传json数据
- 总结
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发送get请求传json数据
在Java中发送GET请求传递JSON数据,可以使用HttpClient库来实现。以下是一个示例代码:
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库创建了一个HttpClient对象,并指定了请求的URL和JSON数据。然后,我们创建了一个HttpGetWithEntity对象,并将JSON数据设置为请求的实体。最后,我们执行GET请求并获取响应的内容。
请注意,这只是一个示例代码,你需要根据你的实际情况进行适当的修改。
总结
到此这篇关于java如何发送get请求获取数据的文章就介绍到这了,更多相关java发送get请求获取数据内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!
您可能感兴趣的文章:
- Java使用get请求接收List集合数据(json)并导出报表问题
- Java restTemplate发送get请求query参数传递问题解决
- JAVA8发送带有Body的HTTP GET请求
- Java webservice的POST和GET请求调用方式
- java后台如何接收get请求传过来的数组