package org.well.well.core.util;



import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;

public class HttpClientUtils {

    /**
     * ���� post������ʱ���Ӧ�ò����ݴ��ݲ�����ͬ���ز�ͬ���<br />
     */
    public static String post(String uri, String body) {
	HttpClient client = new HttpClient();
	client.setTimeout(6000);
	PostMethod method = new PostMethod(uri);
	// for (Map.Entry<String, String> entry : headers.entrySet()) {
	method.addRequestHeader("Content-type",
		"application/json; charset=utf-8");
	method.addRequestHeader("Accept", "application/json");
//	method.addRequestHeader("J-WSSE", JWSSE);
	// method.setEntity(new StringEntity(jsonParam.toString(),
	// Charset.forName("UTF-8")));

	// method.setRequestHeader(entry.getKey(), entry.getValue());
	// method.setRequestHeader("Accept-Charset", "UTF-8");
	// }
	method.setRequestBody(body);
	try {
	    int statusCode = client.executeMethod(method);
	    if (statusCode == HttpStatus.SC_OK) {
		return new String(method.getResponseBody(), "UTF-8");
		// return method.getResponseBodyAsString();
	    }

	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return null;
    }

    /**
     * post�ύ��
     */
    public static String postForm(String uri, String body) {
	HttpClient client = new HttpClient();
	client.setTimeout(6000);
	PostMethod method = new PostMethod(uri);
	method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");//
	method.addRequestHeader("Accept", "application/json");
	method.setRequestBody(body);
	try {
	    int statusCode = client.executeMethod(method);
	    if (statusCode == HttpStatus.SC_OK) {
		return new String(method.getResponseBody(), "UTF-8");
//	    return method.getResponseBodyAsString();
		// return method.getResponseBodyAsString();
	    }

	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return e.getMessage();
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    return e.getMessage();
	}
	return null;
    }

    /**
     * ���� get����
     */
    @SuppressWarnings("deprecation")
    public static String get(String uri) {
	HttpClient client = new HttpClient();
	client.setTimeout(6000);
	GetMethod method = new GetMethod(uri);
	try {
	    int statusCode = client.executeMethod(method);
	    if (statusCode == HttpStatus.SC_OK) {
		return method.getResponseBodyAsString();
	    }

	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return null;
    }

    public static void main(String[] args) throws Exception {
	// try {
	// System.out
	// .println(HTTP
	// .get("http://v.juhe.cn/sms/send?mobile=13691291634&tpl_id=42731&tpl_value=%23username%23%3D%E4%BD%99%E4%BD%B3%E5%BB%BA%26%23startTime%23%3D2017-08-15+14%3A30%26%23endTime%23%3D2017-08-15+17%3A00&dtype=json&key=302dc9a8655b1de25b1e1258db99f4ea"));
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// String uri = "http://wrs.tunnel.qydev.com/ga_webservice/zdryPort";
	//
	// String xml =
	// "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:your=\"http://yourcompany.com/\">"
	// + "<soapenv:Header/>"
	// + "<soapenv:Body>"
	// + "<your:getRyxx>"
	// + " <!--Optional:-->"
	// + "<arg0>1</arg0>"
	// + "<!--Optional:-->"
	// + "<arg1>1</arg1>"
	// + "<!--Optional:-->"
	// + "<arg2>1</arg2>"
	// + "<!--Optional:-->"
	// + "<arg3>1</arg3>"
	// + "</your:getRyxx>"
	// + "</soapenv:Body>" + "</soapenv:Envelope>";
	// Map headers = new HashMap<String, String>();
	// // headers.put("Content-Type", "text/xml;utf-8");
//	String uri = "http://22.192.40.34:8080/qlimg/imgserver/uploadBase64";
//	String body = "data:image/png,"
//		+ Img.img2base64("d:/111.png");
//	String re = HTTP.post(uri, body);
//	System.out.println(re);
    }
}
