com.jts.main.helper.Http.java Source code

Java tutorial

Introduction

Here is the source code for com.jts.main.helper.Http.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jts.main.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * 
 * @author Sammy Guergachi <sguergachi at gmail.com>
 */
public class Http {
    public static String errMsg = "";

    public static String get(String urlString) {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlString);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            StringBuilder buffer = new StringBuilder();
            int read;
            char[] chars = new char[1024];
            while ((read = reader.read(chars)) != -1) {
                buffer.append(chars, 0, read);
            }
            return buffer.toString();
        } catch (MalformedURLException ex) {
            errMsg = ex.getMessage();
        } catch (IOException ex) {
            errMsg = ex.getMessage();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ex) {
                    errMsg = ex.getMessage();
                }
            }
        }
        return "";
    }

    public static String sendPost(String url, URLParameter param) {
        String retval = "";
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpPost request = new HttpPost(My.base_url + url);
            StringEntity params = new StringEntity(param.get());
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            // handle response here...
            retval = org.apache.http.util.EntityUtils.toString(response.getEntity());
            org.apache.http.util.EntityUtils.consume(response.getEntity());
        } catch (IOException | ParseException ex) {
            errMsg = ex.getMessage();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return retval;
    }

    public static String getPost(String url) {
        return getPost(url, new URLParameter());
    }

    public static String getPost(String url, URLParameter param) {
        String retval = "";
        HttpClient httpClient = new DefaultHttpClient();
        try {
            String prm = param.toString();
            HttpPost request = new HttpPost(My.base_url + url);
            StringEntity params = new StringEntity(prm);
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
            // handle response here...
            retval = org.apache.http.util.EntityUtils.toString(response.getEntity());
            org.apache.http.util.EntityUtils.consume(response.getEntity());
        } catch (IOException | ParseException ex) {
            errMsg = ex.getMessage();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return retval;
    }
}