【问题标题】:Received length differs from sent length接收长度与发送长度不同
【发布时间】:2017-12-04 21:36:09
【问题描述】:

我正在使用 Android 应用程序将 base64 编码的字符串发送到 CherryPy 服务器。 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) 的数字不同。

怎么可能?

【问题讨论】:

  • 值是什么???发送了多少字节?
  • 忘记了.close();?
  • @greenapps 确实如此。如果你把它作为一个答案,我会接受它。

标签: android python httpurlconnection cherrypy content-length


【解决方案1】:

也许您忘记使用 out.close(); 关闭流?

【讨论】:

    【解决方案2】:

    您的服务器应该发送一个“关闭标头”,因此客户端不会为他提供结束流。

    【讨论】:

      猜你喜欢
      • 2013-02-06
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 2016-07-01
      • 2018-02-22
      • 2015-08-24
      相关资源
      最近更新 更多