캠퍼스 네트워크 용 로그인 앱을 만들려고합니다. 나는 꽤 멀리 받았지만 지금은 붙어있다.자바와 함께 html 양식 제출
나는 자바 나에게 자동으로 제출해야합니다 간단한 양식을 포함하는 페이지를 반환 한 점에 있어요 :이 문자열을 복사 할 때 .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>
를 제출 클릭, 그것은 기록 나 괜찮아. 지금은 자바에서이 작업을 수행하기 위해 노력하고있어, 그래서 나는 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에 URL을 변경 ($는 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에 대한 자세한 정보를보십시오. 쿠키를 확인하십시오. 세션 ID somwhere가 있어야 할 것입니다. String.getBytes()는 플랫폼 defaultcharset을 사용하여 문자를 바이트로 변환합니다. 문자 세트 (예 : ISO-8859-2)를 지정하십시오. –
반드시 문제와 관련이있는 것은 아니지만 Apache Commons의 HttpClient 클래스를 사용하면이 작업을 수행 할 수 있습니다. - http : //hc.apache.org/httpclient-3.x/ –
저는 이미 DefaultHttpClient를 사용하고 있습니다. 나는 또한 내 코드 상단에'CookieHandler.setDefault (new CookieManager (null, CookiePolicy.ACCEPT_ALL));를 가지고있다. – Dauntless