post方式发送消息提后调os.flush(),请求头的content-length属性会遗失,为什么?

zhlwwj1314 2009-03-18 08:03:09
rt
...全文
372 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kf156 2009-03-20
  • 打赏
  • 举报
回复
我想flush只是将当前缓冲区里的数据发出去,可能系统无法认定你是否还有数据要发送,所以就没带上Content-Length
而直接close的话,那数据量就是固定的了。所以有

个人猜想
leonis01 2009-03-20
  • 打赏
  • 举报
回复
同意楼上说的,调用flush的话数据会被当成chunk,这种情况下,系统在包头里增加Transfer-Encoding,同时将真正的数据长度转成十六进制字符串后拼接上\r\n,塞到原始包体前面.
不要调用flush,直接openInputStream,或者调用任何获取Response信息的函数,都会发送请求,并且带Content-Length.

PS:如果请求数据大于2KB,那一定是当Chunk发的.
zhlwwj1314 2009-03-19
  • 打赏
  • 举报
回复
hcon.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
hcon.setRequestProperty("Content-Type","application/octet-stream");
hcon.setRequestProperty("Content-Length", "" + dataBuf.length);
hcon.setRequestProperty("Accept", "*/*");
os = hcon.openOutputStream();
os.write(dataBuf, 0, dataBuf.length);
os.flush();

这样写的话
抓包显示唯独Content-Length没有发出去
如果去掉os.flush();
所有的头都发出去了
suny_bethere 2009-03-19
  • 打赏
  • 举报
回复
Is there any sample code ?

maybe we can find some issue ..
zhlwwj1314 2009-03-19
  • 打赏
  • 举报
回复
up

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧