CURL POST 请求 返回 Length Required

2015-09-15 23:35:01 +08:00
 eoo
我就纳闷了,头一次出现这问题,百度过了找不到相关问题的解决办法。

携带 cookie GET 请求普通页面倒是没问题, 加了 POST 请求签到地址就返回

Length Required

HTTP Error 411. The request must be chunked or have a content length.


我以为是缺少了 Content-Length 头

就 strlen data 长度加了 header 提交 还是这样。

怎么解决?
11375 次点击
所在节点    PHP
4 条回复
msg7086
2015-09-16 00:53:33 +08:00
Talk is cheap, show me the code.
eoo
2015-09-16 01:04:29 +08:00
@msg7086

<?php
header ("Content-type: text/html; charset=utf-8");

ini_set ('display_errors','on');
error_reporting (E_ALL );

//限制内存占用
ini_set ('memory_limit','10M');



//普通 URL 地址
$url="http://xin123.cc/bbs/book_list.aspx?action=new&siteid=1000";

//签到地址
$qd_url="http://xin123.cc/signin/Signin.aspx?Action=index&Mod=Signin&siteid=1000";


//COOKIE
$cookie = 'sidxin123=6C24EE628EF9AF160_8_68_37964_500100-2-0-0-0-600
';


$data="content=月饼&FaceSelect=1.gif&g=签到";



$ch = curl_init ();
curl_setopt ($ch,CURLOPT_URL,$qd_url );
curl_setopt ($ch,CURLOPT_HEADER,false );
//curl_setopt ($ch,CURLOPT_TIMEOUT,2 );

$header = array (
"Content-length: ".strlen ($data )."
);

curl_setopt ($ch,CURLOPT_HTTPHEADER,$header );
curl_setopt ($ch,CURLOPT_POST,true );
curl_setopt ($ch,CURLOPT_POSTFIELDS,$data );
curl_setopt ($ch,CURLOPT_COOKIE,$cookie );
curl_setopt ($ch,CURLOPT_RETURNTRANSFER,false );
curl_exec ($ch );
curl_close ($ch );
msg7086
2015-09-16 03:04:17 +08:00
<?php
header ("Content-type: text/html; charset=utf-8");
ini_set ('display_errors','on');
error_reporting (E_ALL );
ini_set ('memory_limit','10M');

$url="http://xin123.cc/bbs/book_list.aspx?action=new&siteid=1000";
$qd_url="http://xin123.cc/signin/Signin.aspx?Action=index&Mod=Signin&siteid=1000";
$cookie = 'sidxin123=6C24EE628EF9AF160_8_68_37964_500100-2-0-0-0-600';
$data="content=月饼&FaceSelect=1.gif&g=签到";

$ch = curl_init ();
curl_setopt ($ch,CURLOPT_URL,$qd_url );
curl_setopt ($ch,CURLOPT_HEADER,true );

curl_setopt ($ch,CURLOPT_POST,true );
curl_setopt ($ch,CURLOPT_POSTFIELDS,$data );
curl_setopt ($ch,CURLOPT_COOKIE,$cookie );
curl_setopt ($ch,CURLOPT_RETURNTRANSFER,false );
curl_exec ($ch );
curl_close ($ch );


以上代码测试得到
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 11900
Content-Type: text/html; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=wk1vytudkv1azf55vjd5cbnj; path=/; HttpOnly
Set-Cookie: GUID=409df51603005952; expires=Thu, 15-Sep-2016 19:00:59 GMT; path=/
X-Powered-By: ASP.NET
Date: Tue, 15 Sep 2015 19:00:59 GMT

(内容略)

于是问题在哪?
eoo
2015-09-16 07:23:55 +08:00
@msg7086 奇葩啊, 太奇葩了, 为什么会这么奇葩现在又可以了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/220993

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX