【问题标题】:java post form and get response headers following redirectsjava发布表单并在重定向后获取响应标头
【发布时间】:2015-05-20 10:42:40
【问题描述】:

我正在尝试向登录表单“http://localhost/cilogin/login/”提交 POST 请求并从 JAVA url 连接获取响应标头。登录表单本身在登录到“http://localhost/cilogin/login/success”后会重定向。

我正在尝试通过 JAVA 检测 HTTP 302 重定向。但我只在我获取的响应头中得到 HTTP 200 OK。好像 JAVA 忽略了重定向。 请帮忙。代码如下:

private boolean doLogin(String pass) throws IOException
        {

            String url ="http://localhost/cilogin/login/"; 

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setInstanceFollowRedirects(true);  //you still need to handle redirect manully.
            HttpURLConnection.setFollowRedirects(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Accept-Charset", "UTF-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

            con.connect(); 
            String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
            String param1 = "pd";
            String param2 = pass;
// ...

            String query = String.format("log=%s&pwd=%s&Sub=login", 
        URLEncoder.encode(param1, charset), 
        URLEncoder.encode(param2, charset));


            OutputStream output = con.getOutputStream();
            output.write(query.getBytes(charset));

            int responseCode = con.getResponseCode();

            System.out.println("PASS= "+pass+" code = "+responseCode);

             Map<String, List<String>> map = con.getHeaderFields();

            for (Map.Entry<String, List<String>> entry : map.entrySet())     {

                String key = entry.getKey();
                String val = entry.getValue().get(0);
                if(responseCode == 302 && key.equals("Location") && val.equals("http://localhost/cilogin/login/success"))
                {
                    con.disconnect();
                    return true;
                }
            }

            con.disconnect();
            return false;
        }

java 响应是:

PASS= abc code = 200

这里的“abc”是从外部传递给方法doLogin的字符串

【问题讨论】:

  • 也有可能您的 http 客户端库正在处理重定向,因此您在重定向后的最终请求中只看到 200。
  • 我同意。例如,Apache 的 HttpClient 库也自动处理 GET 重定向。您可以为 HttpClient 库配置任何重定向策略吗?
  • 在 Apache hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/… 的 HttpClient 库中有选项来获取状态码是重定向
  • @fishi,您的建议使我更换了 HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setInstanceFollowRedirects(true);与 con.setInstanceFollowRedirects(false);它就像一个魅力。我要回答我自己的问题
  • @redge,感谢您的有用建议

标签: java php http redirect


【解决方案1】:

正确的代码是:

 private boolean doLogin(String pass) throws IOException
         {

            String url ="http://localhost/cilogin/login/"; 

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            //HttpURLConnection.setFollowRedirects(true);
            con.setInstanceFollowRedirects(false);  //essential for capturing 302 redirect on successful login

            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Accept-Charset", "UTF-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

            con.connect(); 
            String charset = "UTF-8";  // Or in Java 7 and later, use the constant: java.nio.charset.StandardCharsets.UTF_8.name()
            String param1 = "pd";
            String param2 = pass;
// ...

            String query = String.format("log=%s&pwd=%s&Sub=Login", 
            URLEncoder.encode(param1, charset), 
            URLEncoder.encode(param2, charset));


            OutputStream output = con.getOutputStream();
            output.write(query.getBytes(charset));

            int responseCode = con.getResponseCode();

            System.out.println("PASS= "+pass+" code = "+responseCode);

            Map<String, List<String>> map = con.getHeaderFields();

            for (Map.Entry<String, List<String>> entry : map.entrySet())     {

                String key = entry.getKey();
                String val = entry.getValue().get(0);
                //System.out.printf("%s: %s\n", key, val);
                if(responseCode == 302 && key!=null && key.equals("Location") && val.equals("http://localhost/cilogin/login/success"))
                {
                    con.disconnect();
                    return true;
                }
            }

            con.disconnect();
            return false;
        }

【讨论】:

    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多