簡體   English   中英

如何在Android中設置內容長度?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM