http应答 400 Bad Request-因http.header与http.body之间少了空行导致

一、现象:
HTTP 报文是自己组装的。当送的 HTTP body 是空的时候能发成功,但是加上了 HTTP body 后会被 Apache以 http 400 bad request 拒绝掉。
报文如下:

(1)Empty body 时的http请求和应答:


POST /internet/TestProxy HTTP/1.1
Content-Type: text/xml; charset=utf-8
Host: 102.184.80.104:10490
Content-Length: 0

HTTP/1.1 200 OK
Date: Thu, 20 Aug 2020 04:34:24 GMT
Server: UPJAS/3
X-Frame-Options: SAMEORIGIN
Vary: Accept-Encoding,User-Agent
Transfer-Encoding: chunked

0

read:errno=0

(2)non-empty-request:

POST /internet/TestProxy HTTP/1.1
Content-Type: text/xml; charset=utf-8
Host: 102.184.80.104:10490
Content-Length: 3754
<?xml version="1.0" encoding="UTF-8"?>…………

HTTP/1.1 400 Bad Request
Date: Thu, 20 Aug 2020 04:33:22 GMT
Server: Apache
Content-Length: 345
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
read:errno=0

二、原因分析:
telnet通,跟F5的SSL hanshake也完成了,http-body为empty也能到达应用,说明网络啥的肯定都是没问题的,问题应该就出在http报文格式上;
并且是Apache拒掉的,而Apache一般只会解析 http header,所以大概率是 http header 的问题。

把http-header中的HOST为空和Content-Type等问题改过之后,仍被Apache以400 Bad Request拒绝掉。

怀疑到是空行的问题,然后自己在fiddler mock了一个请求,果然 http-header和 http-body 之间没有空行会报错。然后把空行补上是能发通的。

没有空行的报文:
POST http://172.21.6.73:11000/internet/TestProxy HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: 172.21.6.73:11000
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: “”
Content-Length: 3824

<?xml version="1.0" encoding="UTF-8"?> ......……

用 fiddler 的Composer.Raw Execute,会弹窗报错:

没空行时fiddler提示错误

Incomplete Request
The supplied HTTP Request is incomplete. Please insure that there is a single empty line after the HTTP headers.

三、解决办法:
mock http报文时,补上 http-header 和 http-body 之间空行。删除空行后确实已解决问题。

HTTP 请求包含四大部分:
1、请求行(POST http://172.21.6.73:11000/internet/TestProxy HTTP/1.0);
2、请求头
3、空行(http.header和body之间的空行是必须的!)
4、请求体

http请求的四部分:请求行,请求头,空行,请求体

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值