java如何发送请求
发送HTTP请求的方法
在Java中发送HTTP请求可以通过多种方式实现,以下是几种常见的方法:
使用HttpURLConnection
HttpURLConnection是Java标准库中用于发送HTTP请求的类,适用于简单的请求场景。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionExample {
public static void main(String[] args) throws Exception {
URL url = new URL("https://example.com/api");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
使用Apache HttpClient
Apache HttpClient是一个功能更丰富的第三方库,适合复杂的HTTP请求场景。
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("https://example.com/api");
CloseableHttpResponse response = httpClient.execute(request);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
response.close();
httpClient.close();
}
}
使用OkHttp
OkHttp是Square公司开发的现代HTTP客户端,性能优异且易于使用。
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpExample {
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://example.com/api")
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}
}
}
使用Spring RestTemplate
对于Spring项目,可以使用RestTemplate简化HTTP请求的发送。
import org.springframework.web.client.RestTemplate;
public class RestTemplateExample {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject("https://example.com/api", String.class);
System.out.println(result);
}
}
发送POST请求示例
以下是一个使用HttpURLConnection发送POST请求的示例:

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class PostExample {
public static void main(String[] args) throws Exception {
URL url = new URL("https://example.com/api");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
String jsonInputString = "{\"name\":\"John\", \"age\":30}";
try(OutputStream os = con.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
}
}
以上方法各有优缺点,选择哪种取决于项目需求和开发环境。HttpURLConnection适合简单需求且无需额外依赖的场景;Apache HttpClient和OkHttp功能更全面;Spring RestTemplate则适合Spring项目。






