Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何调用从JAVA客户端返回空响应的HEAD请求?_Java_Java 8 - Fatal编程技术网

如何调用从JAVA客户端返回空响应的HEAD请求?

如何调用从JAVA客户端返回空响应的HEAD请求?,java,java-8,Java,Java 8,我试图调用一个API,它返回100作为响应代码和空响应 import org.apache.http.client.methods.HttpHead; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.net.URI; public class SampleController { public sta

我试图调用一个API,它返回100作为响应代码和空响应

import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.net.URI;

public class SampleController {

    public static void main(String[] args) throws Exception {

        try {
            String urlStr = "http://localhost:8080/headrequest";
            HttpHead request = new HttpHead(new URI(urlStr));
            request.addHeader("Expect", "100-continue");

            CloseableHttpClient client = HttpClients.createDefault();
            client.execute(request);
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            throw e;
        }
    }
}
以下是错误:

INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
Aug 19, 2020 3:36:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://localhost:8080
Aug 19, 2020 3:36:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
Aug 19, 2020 3:36:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://localhost:8080
Aug 19, 2020 3:36:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to {}->http://localhost:8080: The target server failed to respond
Aug 19, 2020 3:36:26 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://localhost:8080
Exception in thread "main" org.apache.http.NoHttpResponseException: localhost:8080 failed to respond
Exception: localhost:8080 failed to respond
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
    at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at com.application.controller.SampleController.main(SampleController.java:20)
但是从服务器访问日志。我可以确认API已经执行,服务器返回了100响应代码

“头部/头枕HTTP/1.1”100

因此,从上面的错误中,我了解到apaches HttpClinet没有配置为在服务器发送响应代码100并且仍然希望服务器发送响应时终止连接

import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.net.URI;

public class SampleController {

    public static void main(String[] args) throws Exception {

        try {
            String urlStr = "http://localhost:8080/headrequest";
            HttpHead request = new HttpHead(new URI(urlStr));
            request.addHeader("Expect", "100-continue");

            CloseableHttpClient client = HttpClients.createDefault();
            client.execute(request);
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            throw e;
        }
    }
}
我这里的问题是,是否有java libs在收到100个请求后处理HEAD请求的终止

我确实尝试过谷歌搜索,但没有找到任何解决方案:(

编辑1:我尝试了
HttpURLConnection

private static void testing() throws Exception {
        HttpURLConnection urlConn = null;

        try {
            String urlStr = "http://localhost:8080/headrequest";

            URL url = new URL(urlStr);
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setRequestMethod("HEAD");

            
            urlConn.setRequestProperty("Expect", "100-continue");

            if (urlConn.getResponseCode() != 100) {
                System.out.println("Miss match in the response: " + urlConn.getResponseCode());
            }

        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            throw e;
        }
    }
以下是错误响应:

Exception: Unexpected end of file from server
Exception in thread "main" java.net.SocketException: Unexpected end of file from server
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:851)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:877)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:877)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:848)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:877)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:877)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1593)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at com.application.controller.SampleController.testing(SampleController.java:44)
    at com.application.controller.SampleController.main(SampleController.java:28)

客户机不得在请求中生成100个连续期望值 它不包括消息正文

因此,它的客户端可能希望在收到100个响应后发送消息体,在连接关闭时抛出异常

如果不强制使用apacheclient,下面的简单代码将仅读取带有100个状态代码的响应行并关闭连接

import java.io.*;
导入java.net。*;
类请求程序
{
公共静态void main(字符串参数[])
{
插座;
缓冲剂;
缓冲写入器bw;
字符串a;
字符串响应;
试一试{
s=新套接字(“本地主机”,8080);
br=新的BufferedReader(新的InputStreamReader(新的BufferedInputStream(s.getInputStream())));
bw=新的BufferedWriter(新的OutputStreamWriter(新的BufferedOutputStream(s.getOutputStream())));
写(“头枕/头枕HTTP/1.1”);
换行符();
换行符();
bw.flush();
答复=”;
a=br.readLine();
系统输出打印项次(a);
s、 close();
}
捕获(例外e)
{
系统输出打印ln(e);
}
}
}

是的,这是可以理解的。但是,在java中,如何检查服务器是否返回100。顺便说一句,感谢您的回复。@Finnismissing我已经更新了答案,以包含从服务器读取状态响应的代码。