package cn.net.caoyao.cpzshandset.service.thread.impl.checkAvaiable;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;

import android.os.Handler;
import android.os.Message;

/**
 * 链接服务器测试线程
 * 
 * @author zwj
 *
 *         2015-5-28
 */
public class TestBaiDuThread implements Runnable {
	private Handler mHandler;

	public TestBaiDuThread(Handler mHandler) {
		this.mHandler = mHandler;
		//
	}

	@Override
	public void run() {
		try {
			URL url = new URL("https://www.baidu.com/");
			HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
			// 显示设置的sessionId
			con.setRequestMethod("GET");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setConnectTimeout(3000);// 设置超时为3秒
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			con.connect();
			int responseCode = con.getResponseCode();// 获得连接的状态码
			String result = "";
			if (responseCode == 200) {// 200表示连接服务器成功，且获得正确响应
				LogUtil.i(TConfig.Echo, " 连接服务器成功");
				Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_PROT01_LOGIN_SUCCESS);
				msg.obj = "百度 连接成功";
				mHandler.sendMessage(msg);
			} else {
				// System.out.println("连接服务器失败"+responseCode);
				LogUtil.d(TConfig.Echo, "连接服务器失败");
				   Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
					msg.obj = "百度 连接失败";
					mHandler.sendMessage(msg);
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "百度 连接失败";
			mHandler.sendMessage(msg);
		} catch (IOException e) {
			e.printStackTrace();
			Message msg = mHandler.obtainMessage(RequestConstant.REQUEST_FAIL);
			msg.obj = "百度 连接失败";
			mHandler.sendMessage(msg);
		}
	}

}
