package Net;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;


public class ConnectionHelper {

	private static final String TextUtils = null;

	public ConnectionHelper() {

	}


	public static String httpGet(String url, String[] header, String returnValueCode) {

		String result = "";
		System.out.println("httpGet url------>"+url);

		try {

			HttpGet localHttpGet = new HttpGet(url);

			if (header != null) {
				if (header.length > 0
						&& header.length % 2 == 0) {
					for (int i = 0; i < header.length; i += 2) {
						// //LogUnils.Print("setHead:" + "" + HttpHead[i] + ":"
						// +
						// HttpHead[i + 1]);
						localHttpGet.addHeader(header[i], header[i + 1]);
					}
				}
			}

			BasicHttpParams localBasicHttpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 50 * 1000);
			HttpConnectionParams.setSoTimeout(localBasicHttpParams, 50 * 1000);
			DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams);
			HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet);

			// 闂佸憡甯囬崐鏍蓟閸モ晜瀚氶梺鍨儑濠�鎾煕濠靛棛孝缂併劍鐓￠幃鈺呮嚋绾板瀚瑰ù锝堟閸ㄦ娊鏌ㄥ☉妯肩伇濠殿喗鎮傞獮锟藉ù锝堟閸ㄥ啿鈽夐幙鍕200
			if (localHttpResponse.getStatusLine().getStatusCode() == 200) {

				// 婵炶揪缍�濞夋洟寮ˇ宄瀟Entity闂佸搫鍊介～澶屾兜閸洘鍤旂�瑰嫭澹嗙换渚�寮堕埡鍌涚叆婵炲弶鐗滅槐鎺楀箻鐎甸晲鍑�
				HttpEntity localHttpEntity = localHttpResponse.getEntity();
				if (localHttpEntity != null) {
					InputStream localInputStream = localHttpEntity.getContent();

					BufferedReader localBufferedReader;
					if (null !=TextUtils) {
						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream, returnValueCode), 8192);
					}
					else {
						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream), 8192);
					}

					StringBuilder localStringBuilder = new StringBuilder();

					String line = null;
					try {
						while ((line = localBufferedReader.readLine()) != null) {
							localStringBuilder.append(line+"\n");
						}
						if(localStringBuilder.length()>0)
							localStringBuilder.deleteCharAt(localStringBuilder.length()-1);
						result = localStringBuilder.toString();
					}
					catch (Exception e) {
						e.printStackTrace();
						result = "";
					}
					finally {
						try {
							localBufferedReader.close();
							localInputStream.close();
						}
						catch (Exception e) {
							e.printStackTrace();
							result = "";
						}
					}
				}
			}
			else {
				result = "";
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			//umengSDKInterface..reportError(e);
			result = "";
		}



		return result;

	}

	public static String httpPost(String url, String[] header, byte[] data, String returnValueCode) {

		String result = "";

		try {

			HttpPost localHttpPost = new HttpPost(url);

			if (header != null) {
				if (header.length > 0
						&& header.length % 2 == 0) {
					for (int i = 0; i < header.length; i += 2) {
						// //LogUnils.Print("setHead:" + "" + HttpHead[i] + ":"
						// +
						// HttpHead[i + 1]);
						localHttpPost.addHeader(header[i], header[i + 1]);
					}
				}
			}

			BasicHttpParams localBasicHttpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 30 * 1000);
			HttpConnectionParams.setSoTimeout(localBasicHttpParams, 30 * 1000);
			DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams);

			// 婵烇絽娴傞崰妤呭极閸忚偐鈻旈弶鐐存緲閳诲繘鏌ｉ埗鐑橆��teArrayEntity闂佹寧绋戦悧鍡涙偤瑜旈幊鐐哄磼濞戞瑦顔嶇紓鍌欑鐎氬摜妲愰敓锟�
			// 闂佸憡鐟ラ崐浠嬫晸閽樺鏆為柡瀣暙椤╁ジ鏁撻敓锟�
			// http://www.cnblogs.com/zrcoffee/archive/2012/11/16/2772752.html
			// http://www.cppblog.com/iuranus/archive/2013/05/01/119311.html
			// InputStreamEntity localInputStreamEntity = new
			// InputStreamEntity(new ByteArrayInputStream(data), data.length);
			// localHttpPost.setEntity((HttpEntity) localInputStreamEntity);

			ByteArrayEntity localByteArrayEntity = new ByteArrayEntity(data);
			localHttpPost.setEntity(localByteArrayEntity);

			HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost);

			// 闂佸憡甯囬崐鏍蓟閸モ晜瀚氶梺鍨儑濠�鎾煕濠靛棛孝缂併劍鐓￠幃鈺呮嚋绾板瀚瑰ù锝堟閸ㄦ娊鏌ㄥ☉妯肩伇濠殿喗鎮傞獮锟藉ù锝堟閸ㄥ啿鈽夐幙鍕200
			if (localHttpResponse.getStatusLine().getStatusCode() == 200) {

				// 婵炶揪缍�濞夋洟寮ˇ宄瀟Entity闂佸搫鍊介～澶屾兜閸洘鍤旂�瑰嫭澹嗙换渚�寮堕埡鍌涚叆婵炲弶鐗滅槐鎺楀箻鐎甸晲鍑�
				HttpEntity localHttpEntity = localHttpResponse.getEntity();
				if (localHttpEntity != null) {
					InputStream localInputStream = localHttpEntity.getContent();

					BufferedReader localBufferedReader;
					if (null != TextUtils) {
						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream, returnValueCode), 8192);
					}
					else {
						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream), 8192);
					}

					StringBuilder localStringBuilder = new StringBuilder();

					String line = null;
					try {
						while ((line = localBufferedReader.readLine()) != null) {
							localStringBuilder.append(line);
						}
						result = localStringBuilder.toString();
					}
					catch (Exception e) {
						e.printStackTrace();
						result = "";
					}
					finally {
						try {
							localBufferedReader.close();
							localInputStream.close();
						}
						catch (Exception e) {
							e.printStackTrace();
							result = "";
						}
					}
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			//umengSDKInterface..reportError(e);
			result = "";
		}



		return result;
	}
	
	public static String httpPost(String url, String[] header, String data, String returnValueCode) {

		String result = "";

		try {
	
			HttpPost localHttpPost = new HttpPost(url);
		

			if (header != null) {
				if (header.length > 0
						&& header.length % 2 == 0) {
					for (int i = 0; i < header.length; i += 2) {
						// //LogUnils.Print("setHead:" + "" + HttpHead[i] + ":"
						// +
						// HttpHead[i + 1]);
						localHttpPost.addHeader(header[i], header[i + 1]);
					}
				}
			}

			BasicHttpParams localBasicHttpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 30 * 1000);
			HttpConnectionParams.setSoTimeout(localBasicHttpParams, 30 * 1000);
			DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams);

			// 婵烇絽娴傞崰妤呭极閸忚偐鈻旈弶鐐存緲閳诲繘鏌ｉ埗鐑橆��teArrayEntity闂佹寧绋戦悧鍡涙偤瑜旈幊鐐哄磼濞戞瑦顔嶇紓鍌欑鐎氬摜妲愰敓锟�
			// 闂佸憡鐟ラ崐浠嬫晸閽樺鏆為柡瀣暙椤╁ジ鏁撻敓锟�
			// http://www.cnblogs.com/zrcoffee/archive/2012/11/16/2772752.html
			// http://www.cppblog.com/iuranus/archive/2013/05/01/119311.html
			// InputStreamEntity localInputStreamEntity = new
			// InputStreamEntity(new ByteArrayInputStream(data), data.length);
			// localHttpPost.setEntity((HttpEntity) localInputStreamEntity);

			StringEntity localByteArrayEntity = new StringEntity(data);
			localHttpPost.setEntity(localByteArrayEntity);

			HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost);

			// 闂佸憡甯囬崐鏍蓟閸モ晜瀚氶梺鍨儑濠�鎾煕濠靛棛孝缂併劍鐓￠幃鈺呮嚋绾板瀚瑰ù锝堟閸ㄦ娊鏌ㄥ☉妯肩伇濠殿喗鎮傞獮锟藉ù锝堟閸ㄥ啿鈽夐幙鍕200
			if (localHttpResponse.getStatusLine().getStatusCode() == 200) {

				// 婵炶揪缍�濞夋洟寮ˇ宄瀟Entity闂佸搫鍊介～澶屾兜閸洘鍤旂�瑰嫭澹嗙换渚�寮堕埡鍌涚叆婵炲弶鐗滅槐鎺楀箻鐎甸晲鍑�
				HttpEntity localHttpEntity = localHttpResponse.getEntity();
				if (localHttpEntity != null) {
					InputStream localInputStream = localHttpEntity.getContent();

					BufferedReader localBufferedReader;
					if (null !=TextUtils) {

						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream, returnValueCode), 8192);
					}
					else {
						localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream), 8192);
					}

					StringBuilder localStringBuilder = new StringBuilder();


					String line = null;
					try {
						while ((line = localBufferedReader.readLine()) != null) {
							localStringBuilder.append(line);
						}
						result = localStringBuilder.toString();
					}
					catch (Exception e) {
						e.printStackTrace();
						result = "";
					}
					finally {
						try {
							localBufferedReader.close();
							localInputStream.close();
						}
						catch (Exception e) {
							e.printStackTrace();
							result = "";
						}
					}
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			//umengSDKInterface..reportError(e);
			result = "";
		}


		return result;
	}



}
