Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Created December 17, 2011 06:59
Show Gist options
  • Save goldeneggg/1489517 to your computer and use it in GitHub Desktop.
Save goldeneggg/1489517 to your computer and use it in GitHub Desktop.
HttpUtil.java
package net.shadowapps.commons.util.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;
public class HttpUtil {
private static final String DEFAULT_QUERY_ENCODE = "UTF-8";
public static byte[] getByteArrayFromURL(String strUrl) throws IOException {
return getByteArrayFromURL(strUrl, null, DEFAULT_QUERY_ENCODE, new HttpConfig());
}
public static byte[] getByteArrayFromURL(String strUrl,
HttpConfig httpConfig) throws IOException {
return getByteArrayFromURL(strUrl, null, DEFAULT_QUERY_ENCODE, httpConfig);
}
public static byte[] getByteArrayFromURL(String strUrl,
Map<String, String> queryMap) throws IOException {
return getByteArrayFromURL(strUrl, queryMap, DEFAULT_QUERY_ENCODE, new HttpConfig());
}
public static byte[] getByteArrayFromURL(String strUrl,
Map<String, String> queryMap,
HttpConfig httpConfig) throws IOException {
return getByteArrayFromURL(strUrl, queryMap, DEFAULT_QUERY_ENCODE, httpConfig);
}
public static byte[] getByteArrayFromURL(String strUrl,
String queryEncode) throws IOException {
return getByteArrayFromURL(strUrl, null, queryEncode, new HttpConfig());
}
public static byte[] getByteArrayFromURL(String strUrl,
String queryEncode,
HttpConfig httpConfig) throws IOException {
return getByteArrayFromURL(strUrl, null, queryEncode, httpConfig);
}
public static byte[] getByteArrayFromURL(String strUrl,
Map<String, String> queryMap,
String queryEncode) throws IOException {
return getByteArrayFromURL(strUrl, queryMap, queryEncode, new HttpConfig());
}
public static byte[] getByteArrayFromURL(String strUrl,
Map<String, String> queryMap,
String queryEncode,
HttpConfig httpConfig) throws IOException {
byte[] byteArray = new byte[1024];
byte[] result = null;
HttpURLConnection conn = null;
InputStream in = null;
ByteArrayOutputStream out = null;
int size = 0;
try {
String query = "";
if (queryMap != null && !queryMap.isEmpty()) {
Set<String> keySet = queryMap.keySet();
StringBuilder stb = new StringBuilder();
stb.append("?");
for (String key : keySet) {
if (stb.length() > 1) {
stb.append("&");
}
if (queryEncode != null) {
stb.append(key).append("=").append(URLEncoder.encode(queryMap.get(key), queryEncode));
} else {
stb.append(key).append("=").append(queryMap.get(key));
}
}
query = stb.toString();
}
URL url = new URL(strUrl + query);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(httpConfig.getReadTimeOut());
conn.setConnectTimeout(httpConfig.getConnectTimeOut());
conn.setRequestMethod(httpConfig.getRequestMethod());
for (String headerKey : httpConfig.getRequestHeader().keySet()) {
conn.setRequestProperty(headerKey, httpConfig.getRequestHeader().get(headerKey));
}
conn.setDoInput(true);
conn.connect();
in = conn.getInputStream();
out = new ByteArrayOutputStream();
while ((size = in.read(byteArray)) != -1) {
out.write(byteArray, 0, size);
}
result = out.toByteArray();
} catch (IOException e) {
throw e;
} finally {
try {
if (conn != null) {
conn.disconnect();
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
}
}
return result;
}
public static String encode(String str) throws UnsupportedEncodingException {
return encode(str, DEFAULT_QUERY_ENCODE);
}
public static String encode(String str, String enc) throws UnsupportedEncodingException {
return URLEncoder.encode(str, enc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment