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

StringpostHTTPQuery(String url, String urlParameters)
post HTTP Query
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
...
voidpostJSON(JSONObject job, HttpURLConnection conn)
post JSON
conn.setRequestMethod("POST");
OutputStream os = conn.getOutputStream();
os.write(job.toString().getBytes("UTF-8"));
os.close();
StringpostRaw(URL url, String params)
Posts the given string to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length));
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
...
StringpostURL(final URL url, final String request)
post URL
final HttpURLConnection conn = connect(url, true);
final OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(request);
out.close();
final StringBuilder content = new StringBuilder();
final InputStreamReader in = new InputStreamReader(conn.getInputStream(), "UTF-8");
final char[] buff = new char[1024];
while (true) {
...
Stringput(String url, String content)
put
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("PUT");
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
    bw.write(content);
    bw.flush();
...
intput(URL host, String endpoint, String customer, String name, String version, InputStream in)
put
URL url = new URL(host, endpoint + "?customer=" + customer + "&name=" + name + "&version=" + version);
int responseCode;
HttpURLConnection connection = null;
OutputStream out = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(8192);
...
voidputPOST(HttpURLConnection h, String query)
put POST
OutputStream output = null;
output = h.getOutputStream();
output.write(query.getBytes(C_ENC));
byte[]requestPost(final String remoteUrl, final byte[] content)
request Post
final byte[] buffer = new byte[1024];
final HttpURLConnection connection = (HttpURLConnection) new URL(remoteUrl).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.getOutputStream().write(content);
connection.connect();
if (connection.getResponseCode() >= HttpURLConnection.HTTP_UNAUTHORIZED)
    throw new Exception("HTTP Error Response Code: " + connection.getResponseCode());
...
StringsendJson(URL url, String method, String data)
send Json
final StringBuffer buffer = new StringBuffer();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
if (data != null) {
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/json");
...
voidsendJsonData(final HttpURLConnection connection, final String data)
send Json Data
new OutputStreamWriter(connection.getOutputStream()).append(data).flush();