Java Code Examples for cz.msebera.android.httpclient.client.methods.HttpPost

The following examples show how to use cz.msebera.android.httpclient.client.methods.HttpPost. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: Instagram-Profile-Downloader   Author: beNitinhere   File: InstaUtils.java    License: MIT License 5 votes vote down vote up
public static String login(String username, String pass) throws Exception {
    getInitCookies(ig);

    URLConnection connection = new URL(igAuth).openConnection();
    if(connection instanceof HttpURLConnection){
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setRequestMethod(HttpPost.METHOD_NAME);
        httpURLConnection = addPostHeaders(httpURLConnection);

        String query = new Builder().appendQueryParameter("username", username).appendQueryParameter("password", pass).build().getEncodedQuery();
        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, HTTP.UTF_8));
        bufferedWriter.write(query);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();
        httpURLConnection.connect();

        if(httpURLConnection.getResponseCode() != HttpStatus.SC_OK){
            return "bad_request";
        }

        extractAndSetCookies(httpURLConnection);
        JSONObject jsonObject = new JSONObject(buildResultString(httpURLConnection));
        if(jsonObject.get("user").toString().isEmpty()){
            return BuildConfig.VERSION_NAME;
        }

        return jsonObject.get("authenticated").toString();
    }

    throw new IOException("Url is not valid");
}