方法一:使用HttpURLConnection类调用第三方接口的示例代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class ThirdPartyApiCaller {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("https://api.example.com/endpoint");
// 创建HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 发送请求
int responseCode = connection.getResponseCode();
// 检查响应码
if (responseCode == HttpURLConnection.HTTP_OK) {
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 处理响应内容
System.out.println(response.toString());
} else {
// 处理错误响应
System.out.println("请求失败,响应码:" + responseCode);
}
// 关闭连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码是使用GET方法发送请求
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class ThirdPartyApiCaller {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("https://api.example.com/endpoint");
// 创建HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/json");
// 启用输出流
connection.setDoOutput(true);
// 创建请求体
String requestBody = "{\"param1\":\"value1\", \"param2\":\"value2\"}";
// 将请求体写入输出流
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.write(requestBody.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
// 发送请求
int responseCode = connection.getResponseCode();
// 检查响应码
if (responseCode == HttpURLConnection.HTTP_OK) {
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 处理响应内容
System.out.println(response.toString());
} else {
// 处理错误响应
System.out.println("请求失败,响应码:" + responseCode);
}
// 关闭连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码是使用POST方法发送请求
方法二:使用Apache HttpClient库来调用第三方接口
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class ThirdPartyApiCaller {
public static void main(String[] args) {
try {
// 创建HttpClient对象
HttpClient client = HttpClientBuilder.create().build();
// 创建HttpGet对象
HttpGet request = new HttpGet("https://api.example.com/endpoint");
// 发送请求
HttpResponse response = client.execute(request);
// 检查响应码
if (response.getStatusLine().getStatusCode() == 200) {
// 读取响应内容
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity);
// 处理响应内容
System.out.println(responseBody);
} else {
// 处理错误响应
System.out.println("请求失败,响应码:" + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码使用HttpClient库发送GET请求
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public class ThirdPartyApiCaller {
public static void main(String[] args) {
try {
// 创建HttpClient对象
HttpClient client = HttpClientBuilder.create().build();
// 创建HttpPost对象
HttpPost request = new HttpPost("https://api.example.com/endpoint");
// 设置请求头
request.setHeader("Content-Type", "application/json");
// 创建请求体
String requestBody = "{\"param1\":\"value1\", \"param2\":\"value2\"}";
// 设置请求体
StringEntity entity = new StringEntity(requestBody);
request.setEntity(entity);
// 发送请求
HttpResponse response = client.execute(request);
// 检查响应码
if (response.getStatusLine().getStatusCode() == 200) {
// 读取响应内容
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);
// 处理响应内容
System.out.println(responseBody);
} else {
// 处理错误响应
System.out.println("请求失败,响应码:" + response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上代码使用HttpClient库发送POST请求