gpt4 book ai didi

java - 泽西http客户端: put request with custom Content-Length

转载 作者:行者123 更新时间:2023-12-02 05:36:35 25 4
gpt4 key购买 nike

A想要从InputStream中放入数据,但Jersey不能自动检测数据长度,也没有预设Content-Length header 。

Response destinationResponse = responseWebTarget
.request()
.header(HEADER_X_AUTH_TOKEN, cmd.getOptionValue("dat"))
.put(Entity.entity(inputStream, MediaType.APPLICATION_OCTET_STREAM))

结果是:

INFO: 2 * Sending client request on thread main
2 > PUT http://192.168.1.10:8080/v1/AUTH_system/08138784704e2/test
2 > Content-Type: application/octet-stream
2 > X-Auth-Token: AUTH_tka2d071e5ba3e46e58cbbc3bdcc57bc70

我知道数据长度并尝试手动添加 header :

            Object contentLength = sourceResponse.getHeaders().get(HTTP.CONTENT_LEN).get(0);
Response destinationResponse = responseWebTarget
.request()
.header(HTTP.CONTENT_LEN, contentLength)
.header(HEADER_X_AUTH_TOKEN, cmd.getOptionValue("dat"))
.put(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM))

现在请求看起来不错,但 Jersey 抛出异常:

INFO: 2 * Sending client request on thread main
2 > PUT http://192.168.1.10:8080/v1/AUTH_system/08138784704e2/test
2 > Content-Length: 2760033628
2 > Content-Type: application/octet-stream
2 > X-Auth-Token: AUTH_tka2d071e5ba3e46e58cbbc3bdcc57bc70

Exception in thread "main" javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:472)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:254)
at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:729)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:728)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:421)
at org.glassfish.jersey.client.JerseyInvocation$Builder.put(JerseyInvocation.java:310)
at copier.Main.main(Main.java:118)
Caused by: org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:426)
... 10 more
Caused by: org.apache.http.ProtocolException: Content-Length header already present
at org.apache.http.protocol.RequestContent.process(RequestContent.java:96)
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:132)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:182)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 12 more

如何正确地做到这一点?

最佳答案

我用 Apache HC 替换了 Jersey,

    Long contentLength = Long.parseLong(sourceResponse.getHeaders(HTTP.CONTENT_LEN)[0].getValue());
HttpPut putRequest = new HttpPut(destinationUrl);
putRequest.addHeader(HEADER_X_AUTH_TOKEN, cmd.getOptionValue("dat"));
InputStreamEntity entity = new InputStreamEntity(is, contentLength, ContentType.APPLICATION_OCTET_STREAM);
putRequest.setEntity(entity);

它有 InputStream 包装器,能够指定长度。

PS^ 此外,分块传输编码在 Apache HC 中工作正常,但我无法在 Jersey Client 中打开它。

关于java - 泽西http客户端: put request with custom Content-Length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56161727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com