i want to login to application from java code.
I have found the following code example:
String requestURL = "http://applicationURL/login.jsp";
String data = "email=temail@mail.com&password=123password&login=Login";
public static String sendPostRequest(String data, String requestURL) {
String result="";
try {
// Send the request
URL url = new URL(requestURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.write(data);
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
//Output the response
System.out.println(answer.toString());
result = answer.toString();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}
but i can not login, it returns back only login page.
If anybody can, please help me to understand what am i doing wrong.
I have added method and content-type, but it still does not work:
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");