我正在尝试使用 JAVA URLConnection 类提交表单,但我继续返回一个页面:
meta http-equiv="refresh" content="1;url=/nidp/jsp/err.jsp?sid=0?error=Required+JavaScript+support+is+not+available.&cause=JavaScript+has+been+disabled+or+the+browse+does+not+support+JavaScript.+Use+a+JavaScript+capable+browser+and%2For+enable+JavaScript+support+in+the+browser.">
我怀疑这与我在发送 POST 请求时没有考虑到的 cookie 或 javascript 操作有关。
由于是https页面,无法查看wireshark上的post请求,所以我真的不知道POST数据包是如何离开eclipse的。我确实知道 IE POST 数据包的样子,因为我使用了开发人员的工具来查看请求。
现在的表格:
<form name="frmlogin" action="Login_Chk.aspx?rk=" method="post" onsubmit='return chk_form()' autoComplete="off">
<input type="text" name="txtUser" Class="freeinputl" onKeyPress="return chkAlpha(event,'eng');" />
<input type="text" name="txtId" Class="freeinputl" onKeyPress="return chkDigits(event,'');" />
<inputtype="password" name="txtPass" class="freeinputl" onKeyPress="return chkAlpha(event,'eng');" />
<input type='image' name='btnishur' id='btnishur' tabindex=99 class='hideprint' align=absmiddle src='ok.gif' onmouseover="this.src='ok_over.gif'" onmouseout="this.src='ok.gif'">
<input TYPE="hidden" name="javas" value="0">
<input TYPE="hidden" name="src" value="">
</form>
javascripts(在同一来源):
<script type="text/javascript" src="/Js/Global.js"></script>
<script type="text/javascript" src="js/Form.js"></script>
<script type="text/javascript">
var sw_caps=false;
function setFocus()
{
obj=document.frmlogin;
obj.txtUser.focus();
}
function chk_form()
{
obj=document.forms[0];
if(!chkTextField(obj.txtUser,'Username','yes',2,'','')){return false;}
if(!chkTextField(obj.txtId,'ID','yes',1,'D','')){return false;}
if(!chkTextField(obj.txtPass,'pass','yes',1,'','')){return false;}
return chkFormEndShort(obj);
}
</script>
和我的java代码:
public static void doSubmit(String url, Map<String, String> data) throws Exception {
URL siteUrl = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) siteUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
Set keys = data.keySet();
Iterator keyIter = keys.iterator();
String content = "";
for(int i=0; keyIter.hasNext(); i++) {
Object key = keyIter.next();
if(i!=0) {
content += "&";
}
content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8");
}
System.out.println(content);
out.writeBytes(content);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while((line=in.readLine())!=null) {
System.out.println(line);
}
in.close();
System.out.println("protocol = " + siteUrl.getProtocol());
}
public static void main(String[] args) throws Exception {
Map<String, String> data = new HashMap<String, String>();
data.put("user", "XYZ");
data.put("pass", "xyz");
data.put("id", "123456789");
data.put("src", "");
data.put("javas", "1");
data.put("btnishur.x", "0");
data.put("btnishur.y", "0");
doSubmit("https://www.x.y/Login_Chk.aspx?rk=HTTP/1.1", data);
谢谢!