这是我的代码:无法发送HTTP发送请求到登录表单?
public static String sendPostRequest(String url) {
StringBuffer sb = null;
try {
String data = URLEncoder.encode("user", "UTF-8") + "="
+ URLEncoder.encode("username", "UTF-8") + "&"
+ URLEncoder.encode("password", "UTF-8") + "="
+ URLEncoder.encode("password", "UTF-8");
System.out.println(data); //Just to check if data is correct
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl
.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
DataOutputStream osw = new DataOutputStream(conn.getOutputStream());
osw.writeBytes(data);
osw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String in = "";
sb = new StringBuffer();
while ((in = br.readLine()) != null) {
sb.append(in + "\n");
}
osw.close();
br.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
但每当我试图通过这个HTTP POST请求登录,它返回的HTML代码,这始终是一个包含了网站,而不是我的特定HTML代码,该网页上我的用户页面。在HTML登录表单是这样的:
<td width="100" align="left"><span class="texte_listing_bold">Login:</span></td>
<td width="300" align="center"><input class="form_size1" type="text" name="user"></td>
</tr>
<tr>
<td width="100" align="left"><span class="texte_listing_bold">Password:</span></td>
<td width="300" align="center"><input class="form_size1" type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="login_action" value="true">
<input class="bouton_nosize" type="submit" value="Login">
</td>
通常的按钮具有name元素,我可以通过我的代码,它不工作,让我登录到这样的网站称那些为好,但这韩元因某种原因工作。这是什么原因?
你包裹该HTML到一个