我无法成功完成HTTPS POST请求。请求很顺利,但我得到的回复与我在浏览器中收到的回复不同。我在浏览器中使用拦截代理来查看请求/响应,我相信我在下面的代码中提出完全相同的请求。问题是响应代码是200(OK)而不是响应代码301(重定向)。重定向是我想要的页面,但我似乎无法使用下面的代码到达那里。我尝试过使用'HttpsURLConnection',但它并没有什么区别。
我确定请求是相同的,但它没有被视为相同。可能是在我的浏览器中,SSL证书与我的cookie结合使用,从而使请求不同吗?
如何进入所需的重定向页面?
private static void post(){
try {
URL obj = new URL("https://www.mywebsiteoffun4321.com/add");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Host", "www.mywebsiteoffun4321.com");
con.setRequestProperty("Cookie", cookie);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String urlParameters = "var=true";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
System.out.println(con.getResponseCode());
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (Exception e) {System.out.println(e);}
}
答案 0 :(得分:0)
我发现它是什么。我程序中POST请求的重定向URL与浏览器中的重定向URL不同,即使请求完全相同。
我能够通过完成POST请求,忽略响应,然后对预期的URL执行GET请求来使其工作。这给了我预期的回应。
不确定为什么我的程序中POST请求的重定向不同。