java post form并在重定向后获取响应头

时间:2015-05-20 10:42:40

标签: java php http redirect

我正在尝试向登录表单“http://localhost/cilogin/login/”提交POST请求,并从JAVA网址连接获取响应标头。登录到“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的字符串

1 个答案:

答案 0 :(得分: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;
        }