gpt4 book ai didi

java发布表单并在重定向后获取响应 header

转载 作者:可可西里 更新时间:2023-11-01 17:36:22 25 4
gpt4 key购买 nike

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

我正在尝试通过 JAVA 检测 HTTP 302 重定向。但是我在获取的响应 header 中只得到 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的字符串

最佳答案

正确的代码是:

 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;
}

关于java发布表单并在重定向后获取响应 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30347316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com