接收长度与发送长度不同

2024-09-27 21:24:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Android应用程序向CherryPy服务器发送base64编码的字符串。Android代码的工作原理如下:

URL url = new URL("http://foo.bar/blabla");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(base64s.length());

OutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(base64s.getBytes());

因此,您可以说发送的字节数等于Content-Length头中的字节数。但是,在Python中,当我运行以下命令时:

cl = cherrypy.request.headers['Content-Length']
rawbody = cherrypy.request.body.read()

print "{} bytes, {}".format(len(rawbody), cl)

数字cllen(rawbody)是不同的。你知道吗

怎么会这样?你知道吗


Tags: urlnewclrequestcontentoutconnlength
2条回答

你的服务器应该发送一个“closeheader”,这样客户端就不会为他发送结束流了。你知道吗

也许你忘了用out.close();关闭流?你知道吗

相关问题 更多 >

    热门问题