/**
 *
 */
package org.crazyit.auction.client.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
/**
 * Description:
 * <br/>ÍøÕ¾: <a href="http://www.crazyit.org">·è¿ñavaÁªÃË</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class HttpUtil
{
	// ´´½¨HttpClient¶ÔÏó
	public static HttpClient httpClient = new DefaultHttpClient();
	public static final String BASE_URL =
		"http://192.168.1.88:8888/auction/android/";
	/**
	 *
	 * @param url ·¢ËÍÇëÇóµÄURL
	 * @return ·þÎñÆ÷ÏìÓ¦×Ö·û´®
	 * @throws Exception
	 */
	public static String getRequest(final String url)
		throws Exception
	{
		FutureTask<String> task = new FutureTask<String>(
		new Callable<String>()
		{
			@Override
			public String call() throws Exception
			{
				// ´´½¨HttpGet¶ÔÏó¡£
				HttpGet get = new HttpGet(url);
				// ·¢ËÍGETÇëÇó
				HttpResponse httpResponse = httpClient.execute(get);
				// Èç¹û·þÎñÆ÷³É¹¦µØ·µ»ØÏìÓ¦
				if (httpResponse.getStatusLine()
					.getStatusCode() == 200)
				{
					// »ñÈ¡·þÎñÆ÷ÏìÓ¦×Ö·û´®
					String result = EntityUtils
						.toString(httpResponse.getEntity());
					return result;
				}
				return null;
			}
		});
		new Thread(task).start();
		return task.get();
	}

	/**
	 * @param url ·¢ËÍÇëÇóµÄURL
	 * @param params ÇëÇó²ÎÊý
	 * @return ·þÎñÆ÷ÏìÓ¦×Ö·û´®
	 * @throws Exception
	 */
	public static String postRequest(final String url
		, final Map<String ,String> rawParams)throws Exception
	{
		FutureTask<String> task = new FutureTask<String>(
		new Callable<String>()
		{
			@Override
			public String call() throws Exception
			{
				// ´´½¨HttpPost¶ÔÏó¡£
				HttpPost post = new HttpPost(url);
				// Èç¹û´«µÝ²ÎÊý¸öÊý±È½Ï¶àµÄ»°¿ÉÒÔ¶Ô´«µÝµÄ²ÎÊý½øÐÐ·â×°
				List<NameValuePair> params = 
					new ArrayList<NameValuePair>();
				for(String key : rawParams.keySet())
				{
					//·â×°ÇëÇó²ÎÊý
					params.add(new BasicNameValuePair(key 
						, rawParams.get(key)));
				}
				// ÉèÖÃÇëÇó²ÎÊý
				post.setEntity(new UrlEncodedFormEntity(
					params, "gbk"));
				// ·¢ËÍPOSTÇëÇó
				HttpResponse httpResponse = httpClient.execute(post);
				// Èç¹û·þÎñÆ÷³É¹¦µØ·µ»ØÏìÓ¦
				if (httpResponse.getStatusLine()
					.getStatusCode() == 200)
				{
					// »ñÈ¡·þÎñÆ÷ÏìÓ¦×Ö·û´®
					String result = EntityUtils
						.toString(httpResponse.getEntity());
					return result;
				}
				return null;
			}
		});
		new Thread(task).start();
		return task.get();
	}
}
