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

Stringrequest(String httpUrl, Map httpArgMap)
request
return request(httpUrl, httpArgMap, null);
Listrequest(String url, int timeout, String method, Map header)
request
if (!url.contains("http://") && !url.contains("https://"))
    url = "http://" + url;
URL input = new URL(url);
HttpURLConnection connection = (HttpURLConnection) input.openConnection();
connection.setConnectTimeout(timeout);
connection.setRequestMethod(method);
if (header != null) {
    for (Object key : header.keySet())
...
Stringrequest(String url, Map cookies, Map parameters)
request
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
if (cookies != null && !cookies.isEmpty()) {
    StringBuilder cookieHeader = new StringBuilder();
    for (String cookie : cookies.values()) {
        if (cookieHeader.length() > 0) {
            cookieHeader.append(";");
...
StringrequestData(String url)
request Data
URL obj;
try {
    obj = new URL(url);
} catch (MalformedURLException e) {
    return "";
HttpURLConnection con;
try {
...
StringBuilderrequestDataFromUrl(URL url, byte[] tosend, String userAgent)
request Data From Url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (userAgent != null) {
    urlConnection.setRequestProperty("User-Agent", userAgent);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(20000);
...
voidsaveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
save Http Image
try {
    URL url = new URL(requestUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod(requestMethod);
    conn.setConnectTimeout(5000);
    InputStream inStream = conn.getInputStream();
    byte[] data = readInputStream(inStream);
    FileOutputStream outStream = new FileOutputStream(target);
...
StringsendGet(String url)
send Get
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
    response.append(inputLine);
...
StringsendGet(String url, String param)
send Get
String result = "";
BufferedReader in = null;
HttpURLConnection conn = null;
try {
    String urlNameString = url + "?" + param;
    URL realUrl = new URL(urlNameString);
    conn = (HttpURLConnection) realUrl.openConnection();
    conn.setConnectTimeout(10000);
...
HttpURLConnectionsendGetRequest(String requestURL)
Makes an HTTP request using GET method to the specified URL.
URL url = new URL(requestURL);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoInput(true); 
httpConn.setDoOutput(false); 
return httpConn;
StringsendGetRequest(String url, String cookies)
send Get Request
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.setRequestProperty("Referer", "http://login.sina.com.cn/signup/signin.php?entry=sso");
conn.setRequestProperty("User-Agent",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
BufferedReader read = new BufferedReader(new InputStreamReader(conn.getInputStream(), "gbk"));
String line = null;
...