日期:2014-05-20  浏览次数:20592 次

客户端J2ME用http连接服务器的问题
在连接过程中用模拟器向本地http8080端口发送数据,主要代码如下:
String   strSend   =   "_Arg=1 ";
byte[]   data   =   strSend.getBytes();
private   OutputStream   os   =   null;
String   perRq   =   "http://127.0.0.1:8080/index.htm ";
try   {
        c   =   (HttpConnection)Connector.open(perRq);
        c.setRequestMethod(HttpConnection.POST);
        c.setRequestProperty( "Content-Length ",String.valueOf(data.length));//设置但未发送
        c.setRequestProperty( "Content-Type ",   "application/x-www-form-urlencoded ");
        os   =   c.openOutputStream();
        os.write(data);
        os.flush();  
}
用监听程序在监听8080端口后得到如下数据包,发现并未发送Content-Length属性,而这也就意味着服务器端无法截取得到Content-Length属性,请教怎样才能够保证该属性的发送??还有为什么在数据行的上一行会出现一个记录包头行数的数字?
POST   /index.htm   HTTP/1.1
Content-Type:   application/x-www-form-urlencoded
User-Agent:   UNTRUSTED/1.0
Host:   127.0.0.1:8080
Transfer-Encoding:   chunked

6
_Arg=1

------解决方案--------------------
连续打开好多帖子,忘了刷新看最新内容再回复了
------解决方案--------------------
Transfer-Encoding: chunked //error

A process for decoding the "chunked " transfer-coding (section 3.6)
can be represented in pseudo-code as:

length := 0
read chunk-size, chunk-extension (if any) and CRLF
while (chunk-size > 0) {
read chunk-data and CRLF
append chunk-data to entity-body
length := length + chunk-size
read chunk-size and CRLF
}
read entity-header
while (entity-header not empty) {
append entity-header to existing header fields
read entity-header
}
Content-Length := length
Remove "chunked " from Transfer-Encoding

------------------------------------------------------- rfc2616