Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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 HttpsServer:第一次成功请求后连接超时_Java_Curl_Https_Keep Alive - Fatal编程技术网

Java HttpsServer:第一次成功请求后连接超时

Java HttpsServer:第一次成功请求后连接超时,java,curl,https,keep-alive,Java,Curl,Https,Keep Alive,我正试图将一个小型Web服务器添加到Java程序中,以便向grafana服务器提供内部数据以进行绘图。我访问了内部HttpsServer类,并将其设置为提供所需内容的位置 现在我在命令行上用curl测试程序。它按预期提供第一个请求。但是,如果我再次尝试运行curl命令,就会遇到超时。然后我需要杀死我的java应用程序并重新启动它。就这样,在一个成功的请求之后,它会再次停止工作 编辑:我尝试删除SSL位。普通HTTP似乎没有遇到同样的问题。在curl命令中添加--http1.0似乎也能起作用 我尝

我正试图将一个小型Web服务器添加到Java程序中,以便向grafana服务器提供内部数据以进行绘图。我访问了内部HttpsServer类,并将其设置为提供所需内容的位置

现在我在命令行上用curl测试程序。它按预期提供第一个请求。但是,如果我再次尝试运行curl命令,就会遇到超时。然后我需要杀死我的java应用程序并重新启动它。就这样,在一个成功的请求之后,它会再次停止工作

编辑:我尝试删除SSL位。普通HTTP似乎没有遇到同样的问题。在curl命令中添加--http1.0似乎也能起作用

我尝试的是:

  • 构造函数中的backlog参数已更改-无更改

  • 关闭连接/交换的输出流。我通过尝试使用 试着/抓住/最后

  • “始终使用完整的输入流”-无更改

  • 使用n=>的线程池连接将在n+1请求时挂起

  • 在Exchange处理程序的末尾添加Thread.getCurrentThread().stop()。 显然很讨厌,而且只在某些jvm上工作(Windows:否,Linux:是)

  • 尝试了HttpHandler中的断点,该断点在第一个 请求,但不是第二个。所以我很确定这在外面是个问题 我自己的代码

  • 删除了我自己的代码,并使其尽可能基本。还是一样的问题

我错过什么了吗?我还需要关闭其他东西吗?也许是一些奇怪的“保持生命”的东西?可能套接字没有关闭,但在第二次请求时也不能重用?我不希望配置无限线程(即没有任何限制的CachedThreadPool)。HttpsServer不是适合我的解决方案,我需要切换到undertow或JLHTTP之类的解决方案吗` 谢谢你的帮助

公共类SimpleWebserver{
私有静态最终InetSocketAddress地址=新的InetSocketAddress(8443);
公共静态void main(字符串[]args){
试一试{
//初始化HTTPS服务器
HttpsServer HttpsServer=HttpsServer.create(地址,0);
SSLContext SSLContext=SSLContext.getInstance(“TLS”);
//初始化密钥库
char[]password=“password”.toCharArray();
KeyStore ks=KeyStore.getInstance(“JKS”);
FileInputStream fis=新的FileInputStream(“testkey.jks”);
ks.加载(fis,密码);
//建立密钥管理器工厂
KeyManagerFactory kmf=KeyManagerFactory.getInstance(“SunX509”);
kmf.init(ks,密码);
//建立信任管理器工厂
TrustManagerFactory tmf=TrustManagerFactory.getInstance(“SunX509”);
tmf.init(ks);
//设置HTTPS上下文和参数
sslContext.init(kmf.getKeyManager(),tmf.getTrustManager(),null);
httpsServer.setHttpsConfigurator(新的HttpsConfigurator(sslContext)){
@凌驾
公共无效配置(HttpsParameters参数){
试一试{
//初始化SSL上下文
SSLContext c=SSLContext.getDefault();
SSLEngine引擎=c.创建SSLEngine();
参数setNeedClientAuth(false);
参数setCipherSuite(engine.getEnabledCipherSuite());
setProtocols(engine.getEnabledProtocols());
//获取默认参数
SSLParameters defaultSSLParameters=c.getDefaultSSLParameters();
参数设置SLParameters(默认SSLPParameters);
}捕获(例外情况除外){
Logger.getLogger(SimpleWebserver.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
httpsServer.createContext(“/”,新的SimpleHandler());
//httpsServer.setExecutor(Executors.newFixedThreadPool(5));
//httpsServer.setExecutor(Executors.newCachedThreadPool());
httpsServer.setExecutor(空);
httpsServer.start();
}捕获(例外情况除外){
Logger.getLogger(SimpleWebserver.class.getName()).log(Level.SEVERE,null,ex);
}
}
私有静态类SimpleHandler实现HttpHandler{
@凌驾
公共无效句柄(HttpExchange he)引发IOException{
HttpsExchange HttpsExchange=(HttpsExchange)he;
试一试{
try(InputStream in=httpsExchange.getRequestBody()){
字节[]buf=新字节[500];
while(in.read(buf)!=-1){
}
}
字符串响应=“你好世界!\r\n”;
httpsExchange.sendResponseHeaders(200,response.getBytes().length);
try(OutputStream os=httpsExchange.getResponseBody()){
write(response.getBytes());
}
}最后{
httpsExchange.close();
}
}
}
}
我使用以下curl cmd测试服务器:

curl -v -H "Content-Type: application/json" -d '{"name":"Testing!"}' --insecure --no-keepalive --connect-timeout 5 https://localhost:8443
结果(第一次请求):

* Rebuilt URL to: https://localhost:8443/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (OUT), TLS change cipher, Client hello (1):
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS Unknown, Certificate Status (22):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS Unknown, Certificate Status (22):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using unknown / TLS_AES_256_GCM_SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: C=Unknown; ST=Unknown; L=Unknown; O=Unknown; OU=Unknown; CN=localhost
*  start date: Jul 11 19:57:54 2019 GMT
*  expire date: May 19 19:57:54 2029 GMT
*  issuer: C=Unknown; ST=Unknown; L=Unknown; O=Unknown; OU=Unknown; CN=localhost
*  SSL certificate verify result: self signed certificate (18), continuing anyway.
* (304) (OUT), TLS Unknown, Unknown (23):
> POST / HTTP/1.1
> Host: localhost:8443
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
> 
* upload completely sent off: 19 out of 19 bytes
* (304) (IN), TLS Unknown, Certificate Status (22):
* (304) (IN), TLS handshake, Newsession Ticket (4):
* (304) (IN), TLS Unknown, Unknown (23):
< HTTP/1.1 200 OK
< Date: Sun, 14 Jul 2019 11:27:15 GMT
< Content-length: 15
< 
* (304) (IN), TLS Unknown, Unknown (23):
Hello World! 
* Connection #0 to host localhost left intact
* Rebuilt URL to: https://localhost:8443/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* (304) (OUT), TLS handshake, Client hello (1):
* Operation timed out after 5000 milliseconds with 0 out of 0 bytes received
* stopped the pause stream!
* Closing connection 0
curl: (28) Operation timed out after 5000 milliseconds with 0 out of 0 bytes received