package com.yc.education.util;

import com.aliyuncs.exceptions.ClientException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;

public class HttpSender {

	public static String batchSend(String url, String username, String pwd, String mobile, String content)
			throws Exception {
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod();
		try {
			URI base = new URI(url, false);
			method.setURI(new URI(base, "uoleemApi", false));
			method.setQueryString(
					new NameValuePair[] { new NameValuePair("username", username), new NameValuePair("pwd", pwd),
							new NameValuePair("mobile", mobile), new NameValuePair("content", content) });
			int result = client.executeMethod(method);
			if (result == HttpStatus.SC_OK) {
				InputStream in = method.getResponseBodyAsStream();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = in.read(buffer)) != -1) {
					baos.write(buffer, 0, len);
				}
				return URLEncoder.encode(baos.toString(), "UTF-8");
			} else {
				throw new Exception("HTTP ERROR Status: " + method.getStatusCode() + ":" + method.getStatusText());
			}
		} finally {
			method.releaseConnection();
		}
	}

    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "aG54eQlVmzrWkB1E";
    static final String accessKeySecret = "7jNhd6je6Y172zQ7KrgFxF8YCfZBci";


    //发送短信方法
    public static boolean sendSms(String signname, String templatecode, String phone, String paramstring) throws ClientException {
        return false;
    }

}
