Java Utililty Methods HTTP Request

List of utility methods to do HTTP Request

Description

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

Method

voidaddRequestProperties(final Map requestProperties, final HttpURLConnection connection)
Copies request properties to a connection.
if (requestProperties != null) {
    for (Entry<String, String> entry : requestProperties.entrySet()) {
        connection.addRequestProperty(entry.getKey(), entry.getValue());
StringdoHttpRequest(String action, URL url, String body, String... args)
_more_
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod(action);
connection.setRequestProperty("charset", "utf-8");
for (int i = 0; i < args.length; i += 2) {
    connection.setRequestProperty(args[i], args[i + 1]);
...
StringgetContent(String requestUrl)
get Content
InputStream inputStream = null;
HttpURLConnection httpUrlConn = null;
BufferedReader bufferedReader = null;
InputStreamReader inputStreamReader = null;
try {
    URL url = new URL(requestUrl);
    httpUrlConn = (HttpURLConnection) url.openConnection();
    httpUrlConn.setDoInput(true);
...
StringgetDataFromRequestViaPost(String request, String urlParameters)
Gets data using HTTP POST method.
HttpURLConnection con = (HttpURLConnection) new URL(request).openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
con.setUseCaches(false);
...
StringgetOutputFromUrlConnection(String stringUrl, String requestProperty)
get Output From Url Connection
String outputString = null;
try {
    URL url = new URL(stringUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", requestProperty);
    conn.connect();
    if (conn.getResponseCode() != 200) {
...
StringgetRequest(String urlString)
get Request
StringBuilder result = new StringBuilder();
URL url = null;
BufferedReader rd = null;
HttpURLConnection conn = null;
try {
    url = new URL(urlString);
} catch (MalformedURLException e) {
    e.printStackTrace();
...
HttpURLConnectiongetRequest(URL url, int timeout)
get Request
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
return conn;
StringgetRequestContent(String urlText)
get Request Content
URL url = new URL(urlText);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.setRequestMethod("POST");
urlcon.setUseCaches(false);
urlcon.setDoOutput(true);
PrintStream ps = new PrintStream(urlcon.getOutputStream());
ps.close();
urlcon.connect();
...
StringmakeUrlRequest(String surl, String data, String method)
make Url Request
URL url = new URL(surl);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(method);
con.setRequestProperty("User-Agent", "EBA Client");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
...
voidrequest(boolean quiet, String method, URL url, Map body)
request
if (!quiet) {
    System.out.println("[issuing request: " + method + " " + url + "]");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
byte buffer[] = new byte[8192];
int read = 0;
if (body != null) {
...