2017-04-08 39 views
-1

晚上好。 我試圖做一個用PHP保留的jsp區域。 利用「高級REST cleint」 Chrome瀏覽器,我可以在登錄系統,查看在這樣的頁面的內容:PHP - Scar JSP JSP - HTTP狀態400

1)GET請求www.website.com 頭給我的JSESSIONID,我將使用對於將來的請求

2)GET請求www.website.com/login.jsp

3)POST請求www.website.com/i_security_check 後變量:爲j_username,爲j_password,提交utente

4)GET請求www.website.com/reserved_area.jsp

如果我嘗試做這種「算法」與PHP捲曲,在第三步我得到以下錯誤:HTTP狀態400 - 無效的直接引用,形成的登錄頁面

這是PHP代碼:

<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"https://www.website.com"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 
$step1 = explode("JSESSIONID=", $server_output); 
$step2 = explode("; ", $step1[1]); 

$JSESSIONID = $step2[0]; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"https://www.website.com/login.jsp"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: JSESSIONID=".$JSESSIONID)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://www.website.com/i_security_check"); 
curl_setopt($ch, CURLOPT_POST, 1); 
$data = array("j_password" => "PASSWORD", "j_username" => "USERNAME", "submit" => "Entra", "utente" =>"USERNAME"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: JSESSIONID=".$JSESSIONID, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3", "Connection: keep-alive", "Host: www.website.com", "Referer: https://www.website.com/login.jsp", "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0")); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "www.website.com/reserved_area.jsp"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: JSESSIONID=".$JSESSIONID, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3", "Connection: keep-alive", "Host: www.website.com", "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0")); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
curl_close ($ch); 
?> 

任何想法? 謝謝

回答

0

你不應該手工添加JSESSIONID,但使用捲曲的CURLOPT_COOKIEJARCURLOPT_COOKIEFILE它將爲您處理。你只需要刪除所有行,通過手(CURLOPT_HTTPHEADER)設置cookie,並使用相同的文件在多個請求:作爲CURLOPT_COOKIEJAR

$cookie_jar = tmpfile(); 

// first request, where you **receive** cookie 
.... 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); 

// all calls where you need to already have cookie: 
... 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); 

注不同的選項是用於存儲的cookie,CURLOPT_COOKIEFILE的服務。

// each request 
.... 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); 

文檔:http://php.net/manual/en/function.curl-setopt.php

PS:可以安全地在同一時間,雖然同時使用HTTP代碼400個代表錯誤的請求,而它應該是401(非授權),但是,有一個機會服務器端以這種方式工作。

+0

沒有改善... –

相關問題