Below is my code
String urlParameters = "username=" + URLEncoder.encode("user1", "UTF-8") + "&password=" + URLEncoder.encode("password1", "UTF-8");
URL url = new URL("http://servername/rest/user/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
line = rd.readLine();
while (line != null) {
System.out.println(line);
line = rd.readLine();
}
wr.flush();
wr.close();
connection.disconnect();
But I got the below errors back
java.io.IOException: Server returned HTTP response code: 401 for URL:
http://servername/rest/user/login
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at client.Class1.login(Class1.java:31)
at client.Class1.main(Class1.java:130)
Do you have any idea?
Thanks