![](/img/trans.png)
[英]How to have MappingJacksonHttpMessageConverter set the content-length
[英]How to set content-length in android?
我想通过服务器客户端JSP页面中的request.getContentLength()
获得内容大小。 但是request.getContentLength()总是返回-1,我不为什么?
Android程式码片段:
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
//conn.setChunkedStreamingMode(100);
conn.setConnectTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setReadTimeout(setTimeOut>0?setTimeOut:timeoutConnection);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "keep-alive");
//conn.setRequestProperty("content-length", "10");
//conn.addRequestProperty("content-length", "20");
conn.setFixedLengthStreamingMode(30);
conn.setRequestProperty("Charsert", ENCODING);
conn.setRequestProperty("Content-Type", "multipart/form-data"
+ ";boundary=" + java.util.UUID.randomUUID().toString());
conn.connect();
您正在使用conn.setChunkedStreamingMode(100) ,当content-lenght事先未知时,它将有效地启用100字节块中的块传输编码 。
如果事先知道要在请求正文中发送的内容的长度,请使用conn.setFixedLengthStreamingMode(int len) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.