キャンパスネットワーク用のログインアプリを作成しようとしています。私はかなり遠くになっていますが、今私は立ち往生しています。JavaでHTMLフォームを送信
私はJavaが私に自動提出しなければならない簡単なフォームを含むページを返す時点でだ:それはログに記録し、私が.htmlのファイルにこの文字列をコピーし、提出をクリックすると
<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>
を私はちょうど良いです。今、私はJavaでこれをやろうとしているので、私はリレー状態とSAML応答を抽出し、提出する次のコードを使用します。 - 内部サーバーエラー
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)へのURLを変更します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
)
私はのContentことに気付きます2つのケースで同じ長さのヘッダーはありませんが、なぜそれが何であるのか、それとも何か関係があるのか分かりません。
このエラー500の詳細を確認してください。Cookieを確認してください。セッションIDをsomwhereにする必要があるかもしれません。 String.getBytes()は、プラットフォームのdefaultcharsetを使用して、文字をバイトに変換します。文字セットを指定してください(例:ISO-8859-2)。 –
あなたの問題に必ずしも関連しているわけではありませんが、Apache CommonsのHttpClientクラスを使用してこの種のジョブを実行します.http://hc.apache.org/httpclient-3.x/ –
私は既にDefaultHttpClientを使用しています。私はまた、 'CookieHandler.setDefault(新しいCookieManager(null、CookiePolicy.ACCEPT_ALL));'をコードの先頭に置いています。 – Dauntless