Java HTTP Post executePostWithCredentials(String targetURL, String urlParameters, String userName, String password)

Here you can find the source of executePostWithCredentials(String targetURL, String urlParameters, String userName, String password)

Description

execute Post With Credentials

License

Open Source License

Declaration

public static String executePostWithCredentials(String targetURL, String urlParameters, String userName,
            String password) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Base64;

public class Main {
    public static String executePostWithCredentials(String targetURL, String urlParameters, String userName,
            String password) {/*from w w  w . ja v a 2 s  . c o  m*/
        HttpURLConnection connection = null;
        try {
            //Create connection
            String userPassword = userName + ":" + password;
            String encoded = Base64.getEncoder().encodeToString(userPassword.getBytes("UTF-8"));

            URL url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "Basic " + encoded);
            connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoOutput(true);

            //Send request
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.close();

            //Get Response  
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            StringBuilder response = new StringBuilder(); // or StringBuffer if not Java 5+ 
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();

            if (response.length() == 0) {
                // if nothing is coming back, at least send the response code.
                // a correct commit only returns 200-ok
                response.append(connection.getResponseCode());
            }
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}

Related

  1. doPOST(HttpURLConnection conn, InputStream input)
  2. doPost(String targetURL, String urlParameters, String contentType)
  3. doPost(String theURL, Map nameValuePairs)
  4. doPost(URL url, String body, String... args)
  5. executePost(String url, JSONObject data)
  6. getHttpPOSTConnection(String urlStr, String charSet, Map props, Map params)
  7. httpGet(final String httpurl, final Map... requestProperties)
  8. httpPost(final String httpurl, final String data, final Map... requestProperties)
  9. httpPost(String httpUrl)