我如何明确禁用分块流模式,对HTTP连接在机器人?机器人、明确、模式、HTTP

2023-09-05 05:57:35 作者:中意你

我是从Android 4.0的使用靶向REST Web服务 HttpsURLConnection 。这工作得很好,除非我尝试 POST 的东西。这是相关的code部分:

  connection.setDoOutput(真正的);
   connection.setChunkedStreamingMode(0);

   ByteArrayOutputStream OUT =新ByteArrayOutputStream();
   serializeObjectToStream(满分,对象);
   byte []数组= out.toByteArray();
   。connection.getOutputStream()写(阵列,0,array.length);
 

这将引发以下异常:

  java.net.Htt pretryException:无法重试流HTTP主体
 

从调试,我意识到,输出流我通过获得connection.getOuputStream()的类型是 ChunkedOutputStream 和从挖掘机器人源$ C ​​$ C我计算过,如果一个请求需要重试(无论何种原因),它与上面的异常戳的,因为它的数字,它的没有的使用 RetryableOutputStream ,它希望在那里。

现在的问题是:如何让我的HttpsURLConnection返回这样一个RetryableOutputStream,或者说,我怎么能prevent分块请求编码是否正确?我的认为的我这样做已经有 setChunkedStreamingMode(0),但显然这是不是这样的...

软件工程 个人用 二 软件项目开发过程与管理

没有, java.net.HttpURLConnection中的实施忽略了0流模式或更低的:

 公共无效setChunkedStreamingMode(INT chunkLength){
    [...]
    如果(chunkLength&所述; = 0){
        this.chunkLength = HttpEngine.DEFAULT_CHUNK_LENGTH;
    } 其他 {
        this.chunkLength = chunkLength;
    }
}
 

解决方案

坏消息!解决的办法是不叫 setChunkedStreamingMode()(甚至 setFixedStreamingMode())从客户端code处所有! -1是定长和chunkedLength内部缺省值,并不能设置客户端,因为设置的值低于或等于0,让它默认为 HttpEngine.DEFAULT_CHUNK_LENGTH (或抛出在固定流模式的情况下除外)。

I'm targeting a REST web service from Android 4.0 using HttpsURLConnection. This works fine unless I try to POST something. This is the relevant code section:

   connection.setDoOutput(true);
   connection.setChunkedStreamingMode(0);

   ByteArrayOutputStream out = new ByteArrayOutputStream();
   serializeObjectToStream(out, object);
   byte[] array = out.toByteArray();
   connection.getOutputStream().write(array, 0, array.length);

This throws the following exception:

   java.net.HttpRetryException: Cannot retry streamed HTTP body

From debugging I realized that the output stream I get via connection.getOuputStream() is of type ChunkedOutputStream and from digging in Androids source code I figured that if a request needs to be retried (for whatever reason), it pokes with the above exception, because it figures out that it is not using a RetryableOutputStream that it wants there.

The question is now: How do I make my HttpsURLConnection return such a RetryableOutputStream, or rather, how can I prevent chunked request encoding properly? I thought I did that already with setChunkedStreamingMode(0), but apparently this is not the case...

[edit]

No, the implementation of java.net.HTTPUrlConnection ignores a streaming mode of 0 or lower:

 public void setChunkedStreamingMode(int chunkLength) {
    [...]
    if (chunkLength <= 0) {
        this.chunkLength = HttpEngine.DEFAULT_CHUNK_LENGTH;
    } else {
        this.chunkLength = chunkLength;
    }
}

解决方案

Bummer! The solution is to not call setChunkedStreamingMode() (or even setFixedStreamingMode()) from the client code at all! "-1" are the internal default values for fixedLength and chunkedLength and cannot be set client-side, because setting a value lower or equal to "0" lets it default to HttpEngine.DEFAULT_CHUNK_LENGTH (or throws an exception in the fixed streaming mode case).