-1
我無法成功完成HTTPS POST請求。請求過得很好,但我得到的迴應與我在瀏覽器中收到的回覆不同。我在瀏覽器中使用攔截代理來查看請求/響應,我相信我在下面的代碼中提出了完全相同的請求。問題是響應代碼是200(OK),而不是響應代碼301(重定向)。重定向到我想要的頁面,但我似乎無法使用下面的代碼。我試過使用'HttpsURLConnection',但它沒有什麼區別。Java HTTPS POST請求正在返回響應代碼200當我知道它應該返回302響應代碼
我確定請求是一樣的,但它沒有被一視同仁。難道在我的瀏覽器中,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);}
}
'HttpURLConnection'默認遵循重定向。 – EJP