1

我创建了在 GAE 上运行并连接到特定页面的应用程序,自动登录到该页面,登录后我想接收 html 并处理它。

这是代码中有问题的(writer.write 部分和 connection.connect())部分:

        this.username = URLEncoder.encode(username, "UTF-8");
        this.password = URLEncoder.encode(password, "UTF-8");
        this.login = "login";

        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");

        OutputStreamWriter writer = new OutputStreamWriter(
                connection.getOutputStream());
        writer.write("str_login=" + login + "&str_user=" + username
                + "&str_pass=" + password);
        writer.close();

        connection.connect();

我在建立连接时收到 IOException (connection.connect())。问题是“application/x-www-form-urlencoded”数据。当我向页面传递错误的参数(例如 str_passSSs、str_usernaAAme 或根本没有参数)时,我无法登录,但我确实得到了登录页面的 html 响应。因此,Google App Engine 似乎不支持这种通信。是否可以通过 GAE 支持的其他方式登录此页面?

在 Wireshark 中,我看到用户名和密码作为基于行的文本数据(application/x-www-form-urlencoded)以纯文本形式发送。我知道这不安全,但就是这样。

4

1 回答 1

0

当您调用 getOutputStream() 时,连接已经隐式建立。无需再次调用 connection.connect() 。

此外,不要关闭输出编写器,而是尝试使用 flush()。

作为最佳实践,您应该在 finally 块中关闭、关闭和连接:

InputStream in = null;
OutputStream out = null;
HttpUrlConnection conn = null;

try {
  ...
} catch (IOException ioe) {
  ...
} finally {
  if (in!=null) {try {in.close()} catch (IOException e) {}}
  if (out!=null) {try {out.close()} catch (IOException e) {}}
  if (conn!=null) {try {conn.close()} catch (IOException e) {}} 
} 
于 2013-01-03T09:28:07.267 回答