您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在 Java 中,处理 HTTP 异常通常涉及到捕获和处理 java.net.HttpURLConnection
或第三方库如 Apache HttpClient、OkHttp 等抛出的异常
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main {
public static void main(String[] args) {
try {
URL url = new URL("https://api.example.com/data");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
// 处理响应数据
} else {
// 处理 HTTP 异常
System.err.println("GET request failed");
}
connection.disconnect();
} catch (IOException e) {
// 处理网络异常或其他 IO 异常
e.printStackTrace();
}
}
}
首先,需要添加 Apache HttpClient 库的依赖。如果使用 Maven,可以在 pom.xml
文件中添加以下依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
然后,可以使用以下代码处理 HTTP 异常:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 Main {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
String url = "https://api.example.com/data";
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Accept", "application/json");
HttpResponse httpResponse = httpClient.execute(httpGet);
int responseCode = httpResponse.getStatusLine().getStatusCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
String responseBody = EntityUtils.toString(httpEntity);
// 处理响应数据
} else {
// 处理 HTTP 异常
System.err.println("GET request failed");
}
} catch (IOException e) {
// 处理网络异常或其他 IO 异常
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
首先,需要添加 OkHttp 库的依赖。如果使用 Maven,可以在 pom.xml
文件中添加以下依赖:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
然后,可以使用以下代码处理 HTTP 异常:
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Main {
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
String url = "https://api.example.com/data";
Request request = new Request.Builder()
.url(url)
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// 处理网络异常或其他 IO 异常
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
// 处理响应数据
String responseBody = response.body().string();
} else {
// 处理 HTTP 异常
System.err.println("GET request failed");
}
}
});
}
}
这些示例展示了如何在 Java 中处理 HTTP 异常。请注意,这些示例仅用于演示目的,实际应用中可能需要根据具体需求进行调整。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。