Учусь работать с апи на примере api.owencloud.ru, отправляю JSON пост запрос. Возвращает код 302 и в response получаю null, хотя по идее должен получать JSON ответ, что я делаю не так?
public class authentication {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static String host_url = "http://api.owencloud.ru";
private static String post_authentication = "/v1/auth/open";
private static Person person = new Person("test", "test");
private static String authentication_json = GSON.toJson(person);
private final String USER_AGENT = "Mozilla/5.0";
private void sendPost() throws Exception {
String url = host_url + post_authentication;
URL obj = new URL(null, url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
String urlParameters = authentication_json;
// Send post request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
public static void main(String[] args) throws Exception {
authentication http = new authentication();
System.out.println("\nTesting 2 - Send Http POST request");
http.sendPost();
}
}
class Person {
private String login;
private String password;
public Person(String login, String password) {
this.login = login;
this.password = password;
}
}