Skip to content

Instantly share code, notes, and snippets.

@yujikosuga
Created September 19, 2011 00:46
Show Gist options
  • Save yujikosuga/1225772 to your computer and use it in GitHub Desktop.
Save yujikosuga/1225772 to your computer and use it in GitHub Desktop.
An HTTP utility library in Java
package com.yujikosuga.http;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/**
* This HttpUtils class has static utility methods for HTTP manipulation.
*
* @author Yuji Kosuga
*
*/
public final class HttpUtils {
/**
* Adds HTTP headers specified in the <code>headers</code> parameter. The
* <code>headers</code> is required to be defined in a String 2D array, such as
* <code>{{"name","value"},{"name","value"}}</code>. .
*
* @param conn the instance of HttpURLConnection to which the <code>headers</code> are added
* @param headers the headers to be added
*/
public static synchronized final void addHeadersToConnection(final HttpURLConnection conn,
final String[][] headers) {
if (conn != null && headers != null && headers.length > 0) {
for (String[] header : headers) {
String name = header[0];
if (name == null)
continue;
String value = header[1];
if (value == null)
value = "";
conn.addRequestProperty(name, value);
}
}
}
/**
* Creates the default HTTP URL connection with the specified <code>url</code> and
* <code>method</code>.
*
* @param url the URL to be accessed
* @param method the method
* @return the instance of HttpURLConnection object
* @throws IOException signals that an I/O exception has occurred
*/
public static synchronized final HttpURLConnection createDefaultHttpURLConnection(
final String url, final String method) throws IOException {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
// conn.setRequestProperty("Content-type", "text/plain");
conn.setConnectTimeout(60000);
conn.setRequestMethod(method);
conn.setInstanceFollowRedirects(false);
conn.setDoInput(true);
conn.setDoOutput(true);
return conn;
}
/**
* Constructs a query string from a 2D array made up of a pair of an HTTP parameter and the value.
* For example, <code>{{"a","b"},{"c","d"}}</code> becomes <code>a=b&amp;c=d</code>.
*
* @param params the 2D array of query-strings
* @return a query string
*/
public static synchronized final String joinQueryString(final String[][] params) {
StringBuilder sb = new StringBuilder();
for (String[] param : params) {
if (param.length > 0) {
if (sb.length() > 0)
sb.append("&");
sb.append(param[0]); // Param name
if (param.length > 1)
sb.append("=").append(param[1]); // Param value
}
}
return sb.toString();
}
/**
* Encodes the specified string in HTTP character encoding, the default is UTF-8.
*
* @param str the string to be encoded
* @return the encoded string
* @throws UnsupportedEncodingException if the "UTF-8" is not supported
*/
public static synchronized String urlEncode(final String str) throws UnsupportedEncodingException {
return URLEncoder.encode(str, "UTF-8");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment