【问题标题】:How get html when you use HttpsURLConnection and it return code 302使用 HttpsURLConnection 时如何获取 html 并返回代码 302
【发布时间】:2011-05-13 09:46:28
【问题描述】:

我想在登录表单中连接。我发送连接信息。问题,识别后,网站进行重定向,当我使用方法 connection.getResponseCode() 测试我的 Object HttpsURLConnection 时,我有代码 302(HTTP 状态代码 302:临时重定向。)

  1. 重定向后如何使用我的对象连接获取 html 代码?
  2. 连接后如何使用我的连接在所有站点中导航?

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
    
        request.write("login=ll&password=pp");
        request.flush();
        request.close();            
        String line = "";               
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        //.... get string
    

谢谢你的回答:)

【问题讨论】:

    标签: android http forms post login


    【解决方案1】:

    您可以阅读位置标题以查看您被重定向到的位置

    String header = connection.getHeaderField("location");
    

    然后打开到该 URL 的新连接。

    您很可能被从 HTTPS(登录)重定向到 HTTP(登录后)。请参阅 this answer 到另一个问题,了解为什么 HttpURLConnection 在这种情况下不会自动跟随重定向。

    【讨论】:

    • 感谢获取下一个 url,但是当我在这个地址打开一个新连接时,他没有登录。当我发送错误密码时,我收到错误消息。如果我输入了正确的密码,我就有了重定向。 HttpsURLConnection 如何获取会话?我需要获取其他信息吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多