package apiserver;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.alibaba.fastjson.JSONObject;
public class ApiTest {
private void sendsms() throws Exception {
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod("https://tianqiapi.com/api/sms");
postMethod.getParams().setContentCharset("UTF-8");
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
String appid = "xxxxxxxxxx";
String appsecret = "yyyyyyyyyy";
NameValuePair[] data = {
new NameValuePair("appid", appid),
new NameValuePair("appsecret", appsecret),
new NameValuePair("sign_id ", "你得签名ID"),
new NameValuePair("template_id", "102"),
new NameValuePair("mobile", "手机号码"),
new NameValuePair("code", URLEncoder.encode("1234", "utf-8"))
};
postMethod.setRequestBody(data);
postMethod.setRequestHeader("Connection", "close");
int statusCode = httpClient.executeMethod(postMethod);
System.out.println("statusCode: " + statusCode + ", body: "
+ postMethod.getResponseBodyAsString());
}
public static void main(String[] args) throws Exception {
ApiTest t = new ApiTest();
t.sendsms();
}
}
|