Java 尝试使用 Jersey 客户端 API 执行 POST 请求的“已连接”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28951667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:04:55  来源:igfitidea点击:

'Already Connected' exception trying to do POST request using Jersey Client API

javajerseyclientjboss-arquillian

提问by Marcos J.C Kichel

I'm creating integration tests for a JAX-RS/Jersey Webservice deployed on Tomcat 8 using arquillian.

我正在使用 arquillian 为部署在 Tomcat 8 上的 JAX-RS/Jersey Web 服务创建集成测试。

I am trying to do a POST request like that:

我正在尝试执行这样的 POST 请求:

E dummy = dummyFactory.manufacturePojo(getSubClassType());
dummy.setId(null);

Client client = ClientBuilder.newClient();
WebTarget target = client.target(BASE_URI).path("bandeira");

Response response = target.request(MediaType.APPLICATION_JSON)
            .header(HttpHeaders.AUTHORIZATION, CHAVE_TESTE)
            .header(HttpHeaders.CONTENT_TYPE, "application/json")
            .post(Entity.entity(dummy, MediaType.APPLICATION_JSON));

When I do that I get this exception:

当我这样做时,我得到了这个例外:

Caused by: java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3000)
at org.glassfish.jersey.client.HttpUrlConnector.setOutboundHeaders(HttpUrlConnector.java:364)
at org.glassfish.jersey.client.HttpUrlConnector.access0(HttpUrlConnector.java:91)
at org.glassfish.jersey.client.HttpUrlConnector.getOutputStream(HttpUrlConnector.java:327)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:201)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commitStream(CommittingOutputStream.java:195)
at org.glassfish.jersey.message.internal.CommittingOutputStream.commit(CommittingOutputStream.java:263)
at org.glassfish.jersey.message.internal.OutboundMessageContext.commitStream(OutboundMessageContext.java:816)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:546)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:331)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:243)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
... 149 more

I could use some heuristic since I am still learning arquillian and Jersey client API:) Thank you

我可以使用一些启发式方法,因为我仍在学习 arquillian 和 Jersey 客户端 API:) 谢谢

采纳答案by G. Demecki

It may be that java.lang.IllegalStateException: Already connectedonly masks the SSLHandshakeException. Please take a look at the issue #3000(previously known as JERSEY-2728bug).

可能java.lang.IllegalStateException: Already connected只是掩盖了SSLHandshakeException. 请查看问题#3000(以前称为JERSEY-2728错误)。

回答by YuraHoy

Probably the problem is in SSL negotiation. Try to add "trustall" Client initialization logic.

问题可能出在 SSL 协商中。尝试添加“trustall”客户端初始化逻辑。

SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

} }, new java.security.SecureRandom());

Client client = ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true)
.register(MultiPartFeature.class)
.register(new EncodingFeature("gzip", GZipEncoder.class))
.build();

回答by Hao Ma

This could be due to network connection problem. I met this problem since my VPN lost connection. Exceptions with "Already connected" reported during Hymanson serializing the mail body.(I imported the source code of Jersey and Hymanson-jaxrs-base for debugging). After I removed the mail body, then new Exception with error, "Unknown hostname", came out.

这可能是由于网络连接问题。由于我的 VPN 失去连接,我遇到了这个问题。Already connected在 Hymanson 序列化邮件正文期间报告了“ ”异常。(我导入了 Jersey 和 Hymanson-jaxrs-base 的源代码进行调试)。在我删除邮件正文后Unknown hostname,出现了带有错误“ ”的新异常。

After I login to my VPN, everything works fine.

登录到我的 VPN 后,一切正常。

I am very unhappy about the Jersey client exception "Already connected" which gave me nothing but confusion.

我对泽西岛的客户例外感到非常不满"Already connected”,这让我很困惑。

回答by Manoj

You may be setting header property on http connection after calling connection.connect();

您可能在调用 connection.connect(); 后在 http 连接上设置标头属性;