Java 为什么在发布 JSON 字符串时出现“解析 HTTP 请求标头时出错”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26504212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get "Error parsing HTTP request header" when POSTing a JSON string?
提问by user3686864
I am trying to send a POST
request from browser to my server(local host). My request URL is :
我正在尝试POST
从浏览器向我的服务器(本地主机)发送请求。我的请求网址是:
http://localhost:8080/myPath/myServlet?requestData={ .......//Json String......};
requestData
is a json String (I am using GSON for the purpose.) Everything is working fine until the data in the json string exceeds a particular limit. Say, I am sending array of objects in the json string. If the number of objects in the list exceed 67
then I get following error :
requestData
是一个 json 字符串(我为此使用 GSON。)一切正常,直到 json 字符串中的数据超过特定限制。说,我在 json 字符串中发送对象数组。如果列表中的对象数量超过,67
则会出现以下错误:
AM org.apache.coyote.http11.AbstractHttp11Processor process
INFO: Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
Why is this so? I am totally confused. Why does this happen and what needs to be done to fix this? I want to understand the reason behind this, for I don't understand that after particular number of objects it suddenly stops working and I get this error in my console.
为什么会这样?我完全糊涂了。为什么会发生这种情况,需要采取什么措施来解决这个问题?我想了解这背后的原因,因为我不明白在特定数量的对象之后它突然停止工作并且我在控制台中收到此错误。
回答by AlexR
It seems that you are using POST incorrectly. Although you are using POST you are sending JSON as a request parameter that is the GET style. When using POST you should send content as a request body. In this case no reasonable size limitation exist.
您似乎错误地使用了 POST。尽管您使用的是 POST,但您将 JSON 作为 GET 样式的请求参数发送。使用 POST 时,您应该将内容作为请求正文发送。在这种情况下,不存在合理的大小限制。
回答by nkatsar
I had a similar issue, I was sending a POST request (using RESTClient plugin for Firefox) with data in the request body and was receiving the same message.
我有一个类似的问题,我在请求正文中发送一个带有数据的 POST 请求(使用 RESTClient 插件 for Firefox)并收到相同的消息。
In my case this happened because I was trying to use HTTPS protocol in a local tomcat instance where HTTPS was not configured.
就我而言,发生这种情况是因为我试图在未配置 HTTPS 的本地 tomcat 实例中使用 HTTPS 协议。
回答by Daria
In my case problem was caused by security issues, i used csfr for authentication, and all my post forms should have input with _csrf
在我的情况下,问题是由安全问题引起的,我使用 csfr 进行身份验证,并且我所有的帖子表单都应该输入 _csrf
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
回答by Rudy Vissers
IMHO the problem of parsing the headers can be caused by numerous reasons.
In my case it was due to the fact that the following XML was passed:
恕我直言,解析标题的问题可能由多种原因引起。
就我而言,这是因为通过了以下 XML:
<soapenv:Header/>
The empty header element was generated by SoapUI.
After the removal of the <soapenv:Header/>
from the XML of the WS then everything was fine.
空的标题元素是由 SoapUI 生成的。<soapenv:Header/>
从 WS 的 XML 中删除 之后,一切都很好。
回答by u5525155
I apologize for not answering questions seriously before.
之前没有认真回答问题,我深表歉意。
1.Maybe it caused by special characters.You can get details from this url。https://bz.apache.org/bugzilla/show_bug.cgi?id=60594
1.可能是特殊字符引起的,具体可以到这个网址查看。https://bz.apache.org/bugzilla/show_bug.cgi?id=60594
solve it by:
解决方法:
encodeURIComponent(JSON.stringify(data))
or change your tomcat version to 7.0.67 or lower.
According to developers's opinion, tomcat developers in the following versions set the option to allow |,{,}
.
Get detail http://tomcat.apache.org/tomcat-8.0-doc/config/systemprops.html#Other
或将您的 tomcat 版本更改为 7.0.67 或更低。根据开发者的意见,以下版本的tomcat开发者设置了允许|,{,}
. 获取详细信息http://tomcat.apache.org/tomcat-8.0-doc/config/systemprops.html#Other
The new environment variable will be included in: - trunk for 9.0.0.M18 onwards
新的环境变量将包含在: - 9.0.0.M18 以后的主干
8.5.x for 8.5.12 onwards
8.0.x for 8.0.42 onwards
7.0.x for 7.0.76 onwards2.
8.5.12 以后的 8.5.x
8.0.42 以后的 8.0.x
7.0.x 为 7.0.76 起2。
Another reason maybe that Request header is too large.You can solve this by modifying the server.xml
.
另一个原因可能是请求头太大了server.xml
。可以通过修改.
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150"
minSpareThreads="25" maxSpareThreads="75" enableLookups="false"
redirectPort="8443" acceptCount="100" connectionTimeout="20000"
disableUploadTimeout="true" />
Hope it works for you!
希望这对你有用!
回答by Manish Singh
I fixed this by passing an additional header
我通过传递一个额外的标题来解决这个问题
connection: close, along with the request.
连接:关闭,连同请求。
Could you please try and let me know if this works for you.
你能不能试着让我知道这是否适合你。