Subversion Repositories SmartDukaan

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

package in.shop2020.dtrapi.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

public class HttpClientUtil {
        public static String doGet(String hostName, int fofoId) throws Exception{
                URI uri = new URL("http://"+hostName).toURI();
                HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        HttpGet message = new HttpGet("/fofo/"+fofoId+"/edit");
        HttpResponse response = httpClient.execute(host, message);
        String responseString = toString(response.getEntity().getContent());
        if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
                String errorMessage = String.format("Could not process, responceCode = %d, responseString = %s", response.getStatusLine().getStatusCode(), responseString);
                throw new Exception(errorMessage);
        }
        return responseString;
        }
        
        private static String toString(InputStream inputStream) throws Exception{
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder responseString = new StringBuilder();
                String line = null;
                while((line = bufferedReader.readLine()) != null){
                        responseString.append(line);
                }
                return responseString.toString();
        }
}