這是我的代碼:無法發送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到一個