使用java提交html表单

使用java提交html表单,java,php,forms,Java,Php,Forms,我正在尝试为校园网创建登录应用程序。我已经走得很远了,但现在我被卡住了 java会返回一个包含简单表单的页面,该表单应该自动提交: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <body onload="document.forms[0].submit()"> <noscript> <p> <strong&

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

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>
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(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。这意味着您正在做服务器无法成功处理的事情

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

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

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

顺便说一句,如果您想让事情变得更简单,请访问jakarta.apache.org的用户HttpClient。它支持会话完整模式,所以您不必处理HTTP协议的血淋淋的细节。但如果您愿意,也可以使用常规的HttpConnection\


祝你好运。

尝试获取此错误的更多详细信息。检查饼干。也许你应该有一个会话id。getBytes()使用平台defaultcharset将字符转换为字节。尝试指定字符集(例如ISO-8859-2)。不一定与您的问题有关,但我使用Apache Commons中的类HttpClient来完成这种工作-我已经在使用DefaultHttpClient。我还有
CookieHandler.setDefault(新的CookieManager(null,CookiePolicy.ACCEPT_ALL))在我的代码顶部。+1表示建议Wireshark。这么方便,感觉像作弊!我忘了提到实际的url是https,所以我打赌我不能真正使用WireShark。(我不拥有服务器)另外,我首先运行一个java程序,它以这个html表单字符串结束。然后我将其复制到temp.html文件中,并在浏览器中打开它。当我点击“提交”时,它会让我登录。由于我的浏览器和JRE不共享cookie,我不认为这是一个状态/cookie问题(如果我错了,请纠正我),正如我在其他评论中所说,我使用的是来自apache的DefaultHttpClient。这和你建议的不同吗?