【已解决】DWR-REPLY message: 'The specified call count is not a number'

【问题描述】

今天在用 Python 处理 XHR 请求的时候,POST Request 请求时一直返回以下错误:

//#DWR-REPLY
if (window.dwr) dwr.engine._remoteHandleBatchException({ name:'org.directwebremoting.extend.ServerException', message:'The specified call count is not a number' });
else if (window.parent.dwr) window.parent.dwr.engine._remoteHandleBatchException({ name:'org.directwebremoting.extend.ServerException', message:'The specified call count is not a number' });

【尝试过程】

在网上翻了好久,一开始以为是没有 Session 或者 Session 失效导致出错,但是在添加了 Session 进行请求时,仍然出现上述异常。

其间,参考了这几篇帖子:

  1. The specified call count is not a number
  2. 关于httpWebRequest请求失败异常:The specified call count is not a number
  3. the speicified call count is not a number一种解决

再次地,也尝试了将 content-type 属性,设置为 text/plain,但报错仍然一如既往。

【最终定位】

最后,在查看了 Request 报文的发送形式时,才找到问题所在:

测试中使用的代码,发送的 data 实际上是一个 dict 或者 json 的数据,而不是 content-type 声明的 text/plain 纯文本;而 dict 或者 json 的数据是适配于 application/x-www-form-urlencoded 这种 content-type 的,所以出现了上述问题。

【解决方案】

问题确认了,最后把 POST 请求发送的报文,也构造成相应指定的 text/plain 纯文本,再继续请求时,就成功地获取到所需要的数据了。

url = "http://www.cnblogs.com"
headers = {
    "content-type": "text/plain",
    "key": "value"
}
text = "input some text here..."

requests.post(url, headers=headers, data=text)
posted @ 2020-08-14 01:19  MoonYear530  阅读(943)  评论(0编辑  收藏  举报