我正在嘗試爲校園網絡創建一個登錄應用程序。我已經很遠了,但現在我卡住了。用java提交html表單
我在那裏的Java返回我包含一個簡單的表格應自動提交頁面的一點:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body onload="document.forms[0].submit()">
<noscript>
<p>
<strong>Note:</strong> Since your browser does not support JavaScript, you must press the Continue button once to proceed.
</p>
</noscript>
<form action="url" method="post">
<div>
<input type="hidden" name="RelayState" value="ss:mem:43141e4108638211a81427d145c7e9de"/>
<input type="hidden" name="SAMLResponse" value="base64string"/>
</div>
<noscript>
<div>
<input type="submit" value="Continue"/>
</div>
</noscript>
</form>
</body></html>
當我複製此字符串到一個.html文件,並點擊提交,它記錄我很好。現在,我試圖做到這一點在Java中,所以我提取的RelayState和SAMLResponse和使用下面的代碼提交:
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String params = "RelayState="+relayState+"&SAMLResponse="+saml;
System.out.println(params.length());
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
String fullPage = "";
String s;
while ((s = dis.readLine()) != null)
{
fullPage += s;
}
urlConnection.disconnect();
此代碼返回一個500 - 內部服務器錯誤。我錯過了什麼?
爲了測試正在發送的內容,我改變了鏈接到一個簡單的print_r($ _ POST)和print_r的(getallheaders()可以),這是我得到:
//when I submit using java:
Array(
[Accept] => text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
[Connection] => keep-alive
[Content-Length] => 14176
[Content-Type] => application/x-www-form-urlencoded
[Host] => xxx
[User-Agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
)
Array(
[RelayState] => ss:mem:43141e4108638211a81427d145c7e9de
[SAMLResponse] => base64string
)
//when I submit by copying the string to my browser and clicking submit
Array
(
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[Accept-Encoding] => gzip, deflate
[Accept-Language] => en-us,en;q=0.5
[Connection] => keep-alive
[Content-Length] => 14204
[Content-Type] => application/x-www-form-urlencoded
[DNT] => 1
[Host] => xxx
[User-Agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
)
Array
(
[RelayState] => ss:mem:43141e4108638211a81427d145c7e9de
[SAMLResponse] => base64string
)
我注意到內容 - 在這兩種情況下,長度標題並不相同,但我不知道這是爲什麼,或者與此有什麼關係。
嘗試獲取有關此錯誤500的更多詳細信息。檢查cookie。也許你應該有一個會議ID somwhere。 String.getBytes()使用平臺默認字符串將字符轉換爲字節。嘗試指定字符集(例如ISO-8859-2)。 – 2012-03-01 14:31:13
不一定與您的問題有關,但我使用Apache Commons中的類HttpClient來完成這種工作 - http://hc.apache.org/httpclient-3.x/ – 2012-03-01 14:40:07
我已經使用DefaultHttpClient。我也有'CookieHandler.setDefault(新的CookieManager(null,CookiePolicy.ACCEPT_ALL));'在我的代碼頂部。 – Dauntless 2012-03-01 17:50:40