写了一个模拟post,用来在poj上提交程序,第一次是登入,得到jsession,第二次是提交程序,结果第二次失败了……附上程序和输出的结果,麻烦前辈们看看错在哪里。
很奇怪的是第二次先返回了一个please choose problem,然后又返回了一个400 bad request<pre>
<?php
function request_post($host, $path, $data, $cookie = '', $port = 80)
{
  $header = "POST $path HTTP/1.1\r\n";
  $header .= "Host:$host:$port\r\n";
  $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $header .= "Content-Length: " .strlen($data)."\r\n";
  if ($cookie != '')
    $header .= "Cookie: ".$cookie."\r\n";
  $header .= "Connection: Close\r\n\r\n";
  $header .= $data;
  
  print_r($header);
  echo "\r\n\r\n";
  
  $fp = @fsockopen($host, $port);
  if (!$fp)
  {
    echo "fsockopen failed!";
    die;
  }
  fputs($fp, $header);
  $result = array();
  while (!feof($fp))
  {
    $result[] = fgets($fp);
  }
  fclose($fp);
  return $result;
}$login_result = request_post('poj.org', '/login', "user_id1=zhaojianxuan&password1=123456");
$cookie = substr($login_result[5], 12);
print_r($cookie);
$submit_result = request_post('poj.org', '/submit', "problem_id=1000&language=0&source=%23include+%3Ciostream%3E%0D%0Aint+main%28%29%0D%0A%7B%0D%0A++return+255%3B%0D%0A%7D&submit=Submit", $cookie);
print_r($submit_result);
?>
</pre><pre>POST /login HTTP/1.1Host:poj.org:80Content-Type: application/x-www-form-urlencodedContent-Length: 38Connection: Closeuser_id1=zhaojianxuan&password1=123456JSESSIONID=0D0B42D8CEEC7190EA64ADBEC509CCB8; Path=/POST /submit HTTP/1.1Host:poj.org:80Content-Type: application/x-www-form-urlencodedContent-Length: 132Cookie: JSESSIONID=0D0B42D8CEEC7190EA64ADBEC509CCB8; Path=/Connection: Closeproblem_id=1000&language=0&source=%23include+%3Ciostream%3E%0D%0Aint+main%28%29%0D%0A%7B%0D%0A++return+255%3B%0D%0A%7D&submit=SubmitArray
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: nginx/0.8.52
    [2] => Date: Tue, 20 Mar 2012 07:49:56 GMT
    [3] => Content-Type: text/html;charset=utf-8
    [4] => Connection: keep-alive
    [5] => Content-Length: 1680
    [6] => 
    [7] => <html><head><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="en-US"><title>Error</title><link rel=stylesheet href='poj.css' type='text/css'><link rel='shortcut icon' href='poj.ico'><link rel='icon' href='poj.gif' type='image/gif'><script src='poj.js' language='javascript'></script></head><body lang="en-US" leftmargin=5 link=blue alink=blue vlink=blue onkeydown=KeyDown(event)><a name=top></a><img border="0" src="/JudgeOnline/images/j0293240.wmf" width="50" height="36">
    [8] => <font size="4">Error Occurred</font></p>
    [9] => <ul>
    [10] =>   <li>Please choose problem</li>
    [11] => </ul>
    [12] => <p><img height=30 src=images/home.gif width=40 border=0><font size=3><a href=.>Home Page</a> </font>&nbsp;&nbsp;<img height=30 src='images/goback.gif' width=40 border=0><font size=3><a href='javascript:history.go(-1)'>Go Back</a>&nbsp;&nbsp;<img height=30 width=40 border=0 src='images/top.gif'><a href=#top>To top</a></font><br><hr><P align=center style='font-family:Arial, Helvetica, sans-serif'>All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di<br>Any problem, Please <a href=mailto:[email protected]>Contact Administrator</a></p><script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-790353-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script></body></html>
    [13] => <html>
    [14] => <head><title>400 Bad Request</title></head>
    [15] => <body bgcolor="white">
    [16] => <center><h1>400 Bad Request</h1></center>
    [17] => <hr><center>nginx/0.8.52</center>
    [18] => </body>
    [19] => </html>
)
</pre>

解决方案 »

  1.   

    $cookie = substr($login_result[5], 12);
    你取得的 cookie 对吗?打印出来看看
      

  2.   

    JSESSIONID=0D0B42D8CEEC7190EA64ADBEC509CCB8; Path=/
    应该是对的关键是后来又出现了400 Bad Request
      

  3.   

    //我用的函数function post_url($url, $post = "", $host = "www.ydtuiguang.com", $referrer = 'http://www.ydtuiguang.com/', $proxy = -1){
    if(function_exists("curl_init")){
    $ch = @curl_init();
    @curl_setopt($ch, CURLOPT_URL, $url);
    if(!empty($proxy["address"]))
    @curl_setopt($ch, CURLOPT_PROXY, strpos($proxy["address"], "http") === 0 ? $proxy["address"] : "http://".$proxy["address"]);
    if(!empty($proxy["account"]) && !empty($proxy["password"]))
    @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy["account"].":".$proxy["password"]);
    @curl_setopt($ch, CURLOPT_REFERER, $referrer);
    @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)");
    @curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
    @curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);
    @curl_setopt($ch, CURLOPT_HEADER, 0);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    @curl_setopt($ch, CURLOPT_TIMEOUT, 1000); if (!empty($post)) {
    @curl_setopt($ch, CURLOPT_POST, 1);
    @curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    } $result = @curl_exec($ch);
    @curl_close($ch);
    }elseif(function_exists("fsockopen")){
    $httpheader = "POST ".$url." HTTP/1.1\r\n";   
    $httpheader .= "Accept: */*\r\n";   
    $httpheader .= "Accept-Language: zh-cn\r\n";   
    $httpheader .= "Referer: ".$referrer."\r\n";   
    $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";   
    $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";   
    $httpheader .= "Host: ".$host."\r\n";   
    $httpheader .= "Content-Length: ".strlen($post)."\r\n";   
    $httpheader .= "Connection: Keep-Alive\r\n";   
    $httpheader .= "\r\n";   
    $httpheader .= $post;   
    $fd = fsockopen($host, 80);   
    fwrite($fd, $httpheader);   
    $result = "";   
    while(!feof($fd)){   
    $result .= fread($fd, 8192);   
    }   
    fclose($fd);   
    }elseif(function_existes('file_get_contents')){
    $httpheader = "POST ".$url." HTTP/1.1\r\n";   
    $httpheader .= "Accept: */*\r\n";   
    $httpheader .= "Accept-Language: zh-cn\r\n";   
    $httpheader .= "Referer: ".$referrer."\r\n";   
    $httpheader .= "Content-Type: application/x-www-form-urlencoded\r\n";   
    $httpheader .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";   
    $httpheader .= "Host: ".$host."\r\n";   
    $httpheader .= "Content-Length: ".strlen($post)."\r\n";   
    $httpheader .= "Connection: Keep-Alive\r\n";   

    $opts = array(
       'http'=>array(
    'method'=>"POST",
    'header'=>$httpheader,
    'content'=>$post
       )
     );
    $context = stream_context_create($opts);
    $result = file_get_contents($url, 'r', $context);
    }
    return $result;
    }