我正在尝试为校园网络创建一个登录应用程序。我已经走了相当远,但现在我被卡住了。
我正处于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内部服务器错误。我错过了什么?
为了测试发送的内容,我将url更改为简单的print_r($_POST)和print_r
//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。这意味着您正在执行服务器无法成功处理的操作。
若您没有访问服务器的权限,请尝试以下操作。首先,再次检查从java发送的请求是否(几乎)与从浏览器发送的请求相同。您可以使用WireShark。
如果你认为一切都很好,但仍然不起作用,可能的原因是你是无国籍的。当浏览器第一次到达网站时,它收到作为特殊cookie发送的后台会话ID。Cookie在名为Set-Cookie
的响应标头中发送。浏览器获取此标头的内容,并将其作为请求标头"Cookie"发送回。这就是应用程序和浏览器之间的区别。
从理论上讲,服务器不应该对此请求失败。它应该会再次将您重定向到登录页面。但服务器端似乎存在一个错误:它只是无法处理您的"错误"请求并抛出异常。
顺便说一句,如果你想让事情变得更容易的话,可以使用jakarta.apache.org的HttpClient。它支持会话完全模式,所以你不必处理HTTP协议的血腥细节。但是如果你愿意的话,你也可以支持普通的HttpConnection
祝你好运。