我正在尝试使用HttpURLConnection为j_spring_security_check制作动态动态帖子。这是我的代码:
String urlToGo="http://localhost:8080/projectRef/j_spring_security_check";
String urlParameters ="j_username="+URLEncoder.encode(user.getEmailPro(), "UTF-8")+"&j_password="+URLEncoder.encode(user.getMdpUser(), "UTF-8");
URL url = new URL(urlToGo);
conn.setUseCaches (true);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully.
HttpURLConnection.setFollowRedirects(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while((line=in.readLine())!=null) {
System.out.println(line);
}
in.close();
但是当我执行它时,我得到的只是登录页面的代码。并且身份验证不会处理。 有人可以帮助我吗?