Can';我的java post请求无法登录——可能是html问题

Can';我的java post请求无法登录——可能是html问题,java,html,httprequest,Java,Html,Httprequest,我一直在尝试让这个代码工作,以便它可以发送一个帖子到一个网站,让我登录。我不确定我的java代码或我理解如何阅读html代码的方式是否有问题 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.net.HttpURLConnection; import java.net.MalformedURLException; public

我一直在尝试让这个代码工作,以便它可以发送一个帖子到一个网站,让我登录。我不确定我的java代码或我理解如何阅读html代码的方式是否有问题

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

public class LogIn {
  public static void main(String[] args) {
    String url = "http://www.example.com/login.phtml";

try {

  String destination = "%2Findex.phtml";
  String username = "myUser";
  String password = "myPass";
  String submit = "Log In!";

  String destination_param = "destination";
  String username_param = "username";
  String password_param = "password";
  String submit_param = "submit";

  String encoded_destination = destination_param + "=" + URLEncoder.encode(destination, "UTF-8");
  String encoded_username = username_param + "=" + URLEncoder.encode(username, "UTF-8"); 
  String encoded_password = password_param + "=" + URLEncoder.encode(password, "UTF-8");
  String encoded_submit = submit_param + "=" + URLEncoder.encode(submit, "UTF-8");

  String encoded = encoded_destination + "&" + encoded_username + "&" + encoded_password + "&" + encoded_submit;

    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(encoded);
    osw.flush();

    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String in = "";

    while ((in = br.readLine()) != null) {
      System.out.println(in);   
    }

    osw.close();
    br.close();

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
下面的html代码是登录表单。表单方法旁边写着“action=“/login.phtm”“,您将在我的代码中看到,我已将其添加到我的URL中。这是正确的还是应该是“正确的”?我两次都试过,显然两次都不起作用,所以肯定是别的什么地方出了问题,我想这是输入。您将看到,对于表单中的每个输入,我都创建了一个encodedURL,与我的帖子一起提交,但它似乎不起作用。打印出来的源代码是主网页的源代码,而不是我的帐户的源代码。非常感谢您的帮助,谢谢

<form method="post" action="/login.phtml">
        <input type="hidden" id="templateLoginDest" name="destination"    value="%2Findex.phtml">
        <table style="width: 350px;" align="center">
            <tr>
                <td valign="top" width="100px"><b>Username:</b></td>
                <td valign="top"><input type="text" name="username" id="templateLoginPopupUsername" size="30"></td>
            </tr>
            <tr>
                <td valign="top"><b>Password:</b></td>
                <td valign="top"><input type="password" name="password" size="30"><br><a href="/account/passwordreset.phtml" style="font-size: 8pt;">Forgot Password?</a><br><br></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Log In!"></td>
            </tr>
        </table>
    </form>

用户名:
密码:




什么特别“不起作用”?当我打印输入流时,它会打印网站主页的页面源。如果它工作正常,它应该打印出我的个人资料的页面源,因为我应该登录。程序编译并运行,只需发送正确的信息,以便让我登录即可。你了解“登录”的工作原理吗?我想我了解基本原理,我会发送带有所需标题的帖子,然后他们会验证我的信息,然后按我的要求发送