package com.lloydm.geosword.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

@SuppressWarnings("deprecation")
public class HTTPGet
{
     private final static String TAG = "com.lloydm.geosword.HTTPGet";

     public static String GetURL(String URL)
     {
                     String response = "";

                     HttpParams httpParams = new BasicHttpParams();
                     HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
                     HttpConnectionParams.setSoTimeout(httpParams, 10000);
                     HttpClient httpClient = new DefaultHttpClient(httpParams);
                     HttpPost httpPost = new HttpPost(URL);
                     HttpResponse httpResponse;
                     InputStream ins = null;
                     try
                     {
                                     httpResponse = httpClient.execute(httpPost);
                                     HttpEntity httpEntity = httpResponse.getEntity();
                                     ins = httpEntity.getContent();
                                     BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF8"), 8192);
                                     StringBuilder sb = new StringBuilder();
                                     String line = null;
                                     while ((line = reader.readLine()) != null)
                                     {
                                                     sb.append(line);
                                     }
                                     response = sb.toString();
                                     response.trim();

                     }
                     catch (ClientProtocolException e)
                     {
                                     Log.e(TAG, "ClientProtocolException in HTTPGet");
                                     response = "fail";
                     }
                     catch (IOException e)
                     {
                                     Log.e(TAG, "IOException in HTTPGet:" + URL + " response:" + response);
                                     response = "fail";

                     }
                     finally
                     {
                                     try
                                     {
                                                     if (ins != null)
                                                     {
                                                                     ins.close();
                                                     }
                                     }
                                     catch (IOException e)
                                     {
                                                     Log.e(TAG, "IOException closing stream in HTTPGet");
                                                     response = "fail";
                                     }
                     }
                     return response;
     }
}