0

Использую такой код, но в ответ получаю:

{"code":201, "error":"missing user password}

В чем причина?

public static String httpGet(String url) {
    HttpsURLConnection connection = null;
    InputStream inputStream = null;
    try {
        connection = (HttpsURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setRequestProperty("X-Parse-Application-Id", PARSE_APP_ID);
        connection.setRequestProperty("X-Parse-REST-API-Key", PARSE_REST_API);
        connection.setRequestProperty("X-Parse-Revocable-Session", "1");
        connection.setRequestProperty("username", USERNAME);
        connection.setRequestProperty("password", PASSWORD);
        connection.connect();
        boolean isError = connection.getResponseCode() >= 400;
        inputStream = isError ? connection.getErrorStream() : connection.getInputStream();
        String contentEncoding = connection.getContentEncoding() != null ? connection.getContentEncoding() : "UTF-8";
        return IOUtils.toString(inputStream, contentEncoding); //Apache Commons Lang
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
        if (connection != null) {
            connection.disconnect();
        }
    }
}

2 ответа 2

1

Значения параметров лучше экранировать для передачи в GET запросе:

String query = String.format("username=%s&password=%s", 
    URLEncoder.encode(USERNAME, "UTF-8"), 
    URLEncoder.encode(PASSWORD, "UTF-8"));
1

Нашёл решение, передаём в метод такой url:

https://api.parse.com/1/login?username=USERNAME&password=PASSWORD

и всё заработает.

Из метода убираем:

connection.setRequestProperty("username", USERNAME);
connection.setRequestProperty("password", PASSWORD);

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.