Java 邮政编码不';我似乎无法使用来自HttpURLConnection的表单
我正在尝试使用java登录到一个报告系统。到目前为止,我已经尝试了许多使用HttpURLConnection的不同实现。我试图发布的HTML归结为:Java 邮政编码不';我似乎无法使用来自HttpURLConnection的表单,java,http,post,request,httpurlconnection,Java,Http,Post,Request,Httpurlconnection,我正在尝试使用java登录到一个报告系统。到目前为止,我已经尝试了许多使用HttpURLConnection的不同实现。我试图发布的HTML归结为: <form name="logonForm" method="POST" action="http://remoteserver/logon.object"> <input type="hidden" name="qryStr" value=""> <input type="hidden" name="cmsVisi
<form name="logonForm" method="POST" action="http://remoteserver/logon.object">
<input type="hidden" name="qryStr" value="">
<input type="hidden" name="cmsVisible" value="true">
<input type="hidden" name="authenticationVisible" value="true">
<input type="hidden" name="referer" value="">
<input type="hidden" name="refererFormData" value="">
<input type="hidden" name="isFromLogonPage" value="true">
<input type="text" name="cms" value="xxxxx" class="textfield" id="apsTextEdit">
<input type="text" name="username" value="xxxxx"class="textfield" id="usernameTextEdit">
<input type="text" name="password" value="xxxxx"class="textfield" id="passwordTextEdit">
<select name="authType"id="authenticationSelectBox">
<option selected value='xxxxx'>xxxxx</select>
<input type="submit" name="SUBMIT">
</form>
其中“参数”的范围从零到我发布的HTML中的每个URL编码参数,并且连接设置(目前,就像我尝试其他配置一样)如下所示:
connection = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded; charset=" + "UTF-8");
connection.setUseCaches(false);
connection.connect();
我知道细节是有点稀疏,所以让我知道如果你需要任何更多的信息,非常感谢任何输入 您需要确保还获取所有
元素的name=value
对,以及要在查询字符串中以编程方式“按”的
。按钮只有一个名称,但无论如何,您需要传递它。通过这种方式,服务器端可以区分是否按下了按钮,如果是,则是哪个按钮
此外,如果表单需要会话(它可能使用基于请求的令牌),那么您也需要自己维护cookie。检查并从第一个响应的头中获取Set Cookie
,并将其设置为后续请求的Cookie
头
为便于和较少冗长的HTTP/Frm处理,请考虑./P> 另请参见:
StringBuilder sb = new StringBuilder(baseUrl);
for (Parameter p : parameters) {
if (sb.length() > 0)
sb.append('&');
try {
sb.append(URLEncoder.encode(p.name, "UTF-8"));
if (p.value != null) {
sb.append('=');
sb.append(URLEncoder.encode(p.value, "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
// Not really possible, throw unchecked exception
throw new IllegalStateException("No UTF-8");
}
}
确定要发布哪些参数的最佳方法是在浏览器中捕获发布的跟踪,并查看浏览器是如何执行的。FireBug/DebugBar是您的朋友。为了澄清。。。。为了使我的帖子正常工作,我需要为每个输入(例如qryStr)提供一个名称-值对,即使这些值是空的?还有,提交按钮的名称-值对是如何工作的?是的。这也是一个体面的网络浏览者所做的。提交按钮还有一个
名称
和值
属性。您还需要发送它(就像您对其他每个输入元素所做的那样)。在您的情况下,值
属性将从按钮中省略,但您仍然需要发送名称
。只需在查询字符串中附加SUBMIT=
,表示您已“按下”按钮。另请参见给定的链接。它还包含很多提示和代码示例。@Jon:既然你似乎已经改变了被接受的答案,我很想知道问题的确切原因。还是两者兼而有之?在得到响应之前,您正确地刷新了输出。无需关闭即可获得响应。更重要的是,默认的HttpURLConnection
实现在发送请求之前已经隐式地刷新了输出。。。实际上,我只需要正确地监视浏览器中发生的事情,这就是为什么我将ZZ的答案设置为正确的。然而,我最终还是转到了HttpClient,而您确实提供了总体上更有用的信息。。。再次更改答案…如果名称字段中没有名称字段,则在名称=值中添加什么
StringBuilder sb = new StringBuilder(baseUrl);
for (Parameter p : parameters) {
if (sb.length() > 0)
sb.append('&');
try {
sb.append(URLEncoder.encode(p.name, "UTF-8"));
if (p.value != null) {
sb.append('=');
sb.append(URLEncoder.encode(p.value, "UTF-8"));
}
} catch (UnsupportedEncodingException e) {
// Not really possible, throw unchecked exception
throw new IllegalStateException("No UTF-8");
}
}