使用java提交html表单

Dau*_*ess 5 php java forms

我正在尝试为校园网络创建一个登录应用程序.我已经走得很远,但现在我被卡住了.

我正处于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>
Run Code Online (Sandbox Code Playgroud)

当我将此字符串复制到.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();
Run Code Online (Sandbox Code Playgroud)

此代码返回500内部服务器错误.我错过了什么?

为了测试发送的内容,我将url更改为一个简单的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
)
Run Code Online (Sandbox Code Playgroud)

我注意到这两种情况的内容长度标题不一样,但我不知道为什么会这样,或者它与它有什么关系.

Ale*_*exR 2

如果您有权访问服务器日志,它会有所帮助。当内部服务器发生错误时,会抛出 Status 500。这意味着您正在执行服务器无法成功处理的操作。

如果您无权访问服务器,请尝试以下操作。首先,再次检查从 java 发送的请求是否(几乎)与从浏览器发送的请求相同。为此,您可以使用 WireShark。

如果您认为一切正常,但仍然不起作用,可能的原因是您是无国籍的。当浏览器第一次到达该站点时,它会收到作为特殊 cookie 发送的返回会话 ID。Cookie 在名为 的响应标头中发送Set-Cookie。浏览器获取此标头的内容并将其作为请求标头“Cookie”发送回。这就是您的应用程序和浏览器之间的区别。

理论上,服务器不应在此请求上失败。它应该只是再次将您重定向到登录页面。但服务器端似乎存在一个错误:它无法处理您的“错误”请求并引发异常。

顺便说一句,如果你想让事情变得更容易,请使用来自 jakarta.apache.org 的 HttpClient。它支持会话完整模式,因此您不必处理 HTTP 协议的血腥细节。但如果您愿意,您也可以使用常规 HttpConnection。\

祝你好运。