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

如何获得服务器返回的XML信息的大小并
获得服务器返回的信息:
HTTP   /1.1   200   OK
Date:   Apr   11   2006   15:32:08   GMT
Server:   Apache/2.0.46(win32)
Content-Length:   119
Content-Type:   text/html

<?xml   version= "1.0 "   encoding= "GBK "?>
<root>
<head>
<no1> hello! </no1>
<no2> welcome! </no2>
</head>
<body>
<list> thisisvalue </list>
<list> 这个是可以循环的多个接点 </list>
</body>
</root>

然后,如何判断以上信息的大小,并重新组织信息为:
HTTP   /1.1   200   OK
Date:   Apr   11   2006   15:32:08   GMT
Server:   Apache/2.0.46(win32)
Content-Length:   119
Content-Type:   text/html

000125 <?xml   version= "1.0 "   encoding= "GBK "?>
<root>
<head>
<no1> hello! </no1>
<no2> welcome! </no2>
</head>
<body>
<list> thisisvalue </list>
<list> 这个是可以循环的多个接点 </list>
</body>
</root>

000125为服务器返回信息的长度加6位字节长度.
这个怎么实现啊????

------解决方案--------------------
在servlet中做很容易,如果是自己用socket写的话,就要注意:
1:http的头最后是以两个回车换行结束
2:Content-Length的长度是返回内容的长度(不包含头)

ps:你上面的Content-Type是不是应该为:text/xml
------解决方案--------------------
http连接是一个短连接来的,先从socket读取这次发送过来的所有字节;然后转成字符串;在字符串重查找Content-Length,得出包体的长度;再查找出 <?xml的位置,在前面加上你要求的长度。
------解决方案--------------------
当求天不灵,求地不应的时候,我会自己编写一个工具类解决,哇哈哈!
所以,想想自己学的知识,写个工具类试试吧!