Java Utililty Methods HTTP Post

List of utility methods to do HTTP Post

Description

The list of methods to do HTTP Post are organized into topic(s).

Method

voiddoHttpPost(URL url, String requestBody)
Executes a HTTP-POST Request
HttpURLConnection connection = null;
OutputStreamWriter writer = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Length", String.valueOf(requestBody.length()));
    writer = new OutputStreamWriter(connection.getOutputStream());
...
StringdoPOST(HttpURLConnection conn, InputStream input)
do POST
conn.setRequestMethod("POST");
return call(conn, input);
byte[]doPost(String targetURL, String urlParameters, String contentType)
executes a post to the targetURL.
URL url;
HttpURLConnection connection = null;
try {
    url = new URL(targetURL);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", contentType);
    connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
...
StringdoPost(String theURL, Map nameValuePairs)
do Post
URL url = new URL(validateURL(theURL));
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
postRequest(conn, nameValuePairs);
return readResponse(conn);
StringdoPost(URL url, String body, String... args)
_more_
return doHttpRequest("POST", url, body, args);
StringexecutePost(String url, JSONObject data)
execute Post
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
...
StringexecutePostWithCredentials(String targetURL, String urlParameters, String userName, String password)
execute Post With Credentials
HttpURLConnection connection = null;
try {
    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");
...
HttpURLConnectiongetHttpPOSTConnection(String urlStr, String charSet, Map props, Map params)
get Http POST Connection
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) {
    String k = (String) iterator.next();
    connection.addRequestProperty(k, props.get(k));
connection.setRequestMethod("POST");
connection.setDoOutput(true);
...
InputStreamhttpGet(final String httpurl, final Map... requestProperties)
Simple http get imlementation.
HttpURLConnection connection = (HttpURLConnection) new URL(httpurl).openConnection();
for (Map<String, String> props : requestProperties) {
    addRequestProperties(props, connection);
return connection.getInputStream();
InputStreamhttpPost(final String httpurl, final String data, final Map... requestProperties)
Simple http post implementation.
byte[] bytes = data.getBytes("utf-8");
java.net.URL url = new java.net.URL(httpurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(bytes.length));
...