Java客户端处理返回100状态码空响应的HEAD请求异常问题
First, let’s break down why you’re hitting those exceptions: HTTP 100 Continue is designed as an interim response—it’s meant to tell clients "you can send your request body now" (typically for POST/PUT requests with large payloads). But HEAD requests don’t have a request body, so your server correctly returns 100 and closes the connection immediately. Standard clients like Apache HttpClient and HttpURLConnection are hardcoded to wait for a final HTTP response after receiving 100 Continue, hence the NoHttpResponseException/SocketException when the connection drops unexpectedly.
Here are two reliable ways to resolve this:
1. Use OkHttp (Recommended for Flexibility)
OkHttp gives you fine-grained control over response handling, so you can detect the 100 status code and terminate the connection gracefully without exceptions.
First, add OkHttp to your project (Maven example):
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.12.0</version> <!-- Use the latest stable version --> </dependency>
Then implement a custom interceptor to handle the 100 response:
import okhttp3.*; import java.io.IOException; public class HeadRequestHandler { public static void main(String[] args) throws IOException { OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request modifiedRequest = chain.request() .newBuilder() .addHeader("Expect", "100-continue") .build(); Response response = chain.proceed(modifiedRequest); if (response.code() == 100) { // Close the connection immediately since no final response is coming response.close(); // Return a synthetic success response to avoid exceptions return new Response.Builder() .request(modifiedRequest) .protocol(Protocol.HTTP_1_1) .code(100) .message("Continue") .build(); } return response; } }) .build(); Request headRequest = new Request.Builder() .url("http://localhost:8080/headrequest") .head() .build(); try (Response response = client.newCall(headRequest).execute()) { if (response.code() == 100) { System.out.println("Received 100 Continue, connection closed successfully"); } else { System.out.println("Unexpected response code: " + response.code()); } } } }
This interceptor catches the 100 response, closes the connection cleanly, and returns a synthetic response so your code doesn’t throw an error.
2. Customize Apache HttpClient Behavior
If you need to stick with Apache HttpClient, you can add a custom response interceptor to signal the client to stop waiting for a final response after receiving 100:
import org.apache.http.HttpResponse; import org.apache.http.HttpResponseInterceptor; import org.apache.http.client.methods.HttpHead; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HttpContext; import java.net.URI; public class ApacheClient100Fix { public static void main(String[] args) throws Exception { CloseableHttpClient client = HttpClients.custom() .addInterceptorLast((HttpResponseInterceptor) (response, context) -> { if (response.getStatusLine().getStatusCode() == 100) { // Tell the client to abort further processing context.setAttribute("http.response.abort", Boolean.TRUE); } }) .build(); String urlStr = "http://localhost:8080/headrequest"; HttpHead request = new HttpHead(new URI(urlStr)); request.addHeader("Expect", "100-continue"); try { client.execute(request); System.out.println("Received 100 Continue, connection handled successfully"); } catch (Exception e) { // Ignore abort-specific exceptions since we intentionally stopped the connection if (!(e.getMessage().contains("Connection aborted") || e instanceof org.apache.http.NoHttpResponseException)) { throw e; } } finally { client.close(); } } }
Note: You’ll need to filter out the abort-related exceptions, as the client will still throw an error when we force the connection to close—but these are safe to ignore for this use case.
Quick Note
While using 100 Continue with HEAD requests is non-standard (since HEAD has no request body), these solutions adapt the client to match your server’s behavior. OkHttp is generally easier to customize for edge cases like this compared to Apache HttpClient or HttpURLConnection.
内容的提问来源于stack exchange,提问作者Finn is missing




