Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine 在建立java.net连接时获取GAE上的IOException_Google App Engine_Post_Login - Fatal编程技术网

Google app engine 在建立java.net连接时获取GAE上的IOException

Google app engine 在建立java.net连接时获取GAE上的IOException,google-app-engine,post,login,Google App Engine,Post,Login,我创建了一个在GAE上运行并连接到特定页面的应用程序,自动登录到此页面,登录后我希望接收html并处理它 下面是代码中有问题的部分(writer.write部分和connection.connect()): this.username = URLEncoder.encode(username, "UTF-8"); this.password = URLEncoder.encode(password, "UTF-8"); this.login = "

我创建了一个在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_pasSSs、str_usernaAAme或根本没有参数)时,我无法登录,但我确实会收到登录页面的html响应。所以,谷歌应用引擎似乎不支持这种交流。是否可以以GAE支持的其他方式登录此页面


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

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

另外,请尝试flush()而不是关闭输出编写器

作为一种最佳实践,您应该在最后一个街区接近入口、出口和连接:

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) {}} 
} 

您是否尝试过设置内容类型?是的,我尝试过,但没有帮助。问题是,当我在本地运行应用程序(localhost)时,根本没有问题。事实上,我看到了这个连接。connect()在Google App EngineOk上抛出IOException。我终于解决了这个问题@d4n3全部归功于您,尽管上述帖子并不能直接解决问题,但您给了我一些有用的提示。首先,有些句子是不必要的,在我重写代码后,我得到了异常“太多重定向”。我必须手动管理重定向,还必须手动管理cookies(这就是为什么我可以在本地登录,但无法在GAE上登录)。之后,一切正常。