Could you, please, help me to write an appropriate HTTP POST request in JAVA to authenticate in YouTrack using their REST API? It should look like this (taken from YouTrack Documentation):
POST http://localhost:8081/rest/user/login
Content-Length: 24
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Cookie:
login=root&password=root
I have written a below code to open connection, send a cookie and handle a server response:
URL url = new URL(webPage);
URLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setDoOutput(true);
urlc.setDoInput(true);
((HttpURLConnection) urlc).setRequestMethod("POST");
urlc.setRequestProperty("Content-Length", Integer.toString(24));
urlc.setRequestProperty("Connection", "keep-alive");
urlc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String myCookie = "login=root&password=root";
urlc.setRequestProperty("Cookie", myCookie);
InputStream is = urlc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
String result = sb.toString();
System.out.println("result = " + result);
But I'm getting HTTP response 400. Please tell me what am I doing wrong? I would be really grateful if you suggest a decision to the problem. Thank you!