SMS短信发送的源码分析
其中包含了使用 HttpClient 发送POST请求的方法:
第一步: HTTPClient httpclient = new DefaultHTTPClient()
第二步: HTTPRequest request = new HTTPRequest(HTTPMethod.POST, url)
第三步: request.addHeader("Conetent-Type", "application/json")
第四步: request.addQueryParameter("key",value)
第五步: request.setConnectionTimeout(毫秒) //设置http连接的超时时间
第六步: request.setRequestTimeout(毫秒) //设置http请求的超时时间
第七步: request.setBody(JSONObject body)
后续: 获取结果:
try {
HTTPResponse res = this.httpclient.fetch(req);
this.handleError(res);
return (new SmsSingleSenderResult()).parseFromHTTPResponse(res);
} catch (URISyntaxException var14) {
throw new RuntimeException("API url has been modified, current url: " + this.url);
}
源码分析
public class SmsSingleSender extends SmsBase {
private String url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
public SmsSingleSender(int appid, String appkey) {
super(appid, appkey, new DefaultHTTPClient());
}
public SmsSingleSender(int appid, String appkey, HTTPClient httpclient) {
super(appid, appkey, httpclient);
}
public SmsSingleSenderResult send(int type, String nationCode, String phoneNumber, String msg, String extend, String ext) throws HTTPException, JSONException, IOException {
long random = SmsSenderUtil.getRandom();
long now = SmsSenderUtil.getCurrentTime();
JSONObject body = (new JSONObject()).put("tel", (new JSONObject()).put("nationcode", nationCode).put("mobile", phoneNumber)).put("type", type).put("msg", msg).put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now, phoneNumber)).put("time", now).put("extend", SmsSenderUtil.isNotEmpty(extend) ? extend : "").put("ext", SmsSenderUtil.isNotEmpty(ext) ? ext : "");
HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.url)).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random).setConnectionTimeout(60000).setRequestTimeout(60000).setBody(body.toString());
try {
HTTPResponse res = this.httpclient.fetch(req);
this.handleError(res);
return (new SmsSingleSenderResult()).parseFromHTTPResponse(res);
} catch (URISyntaxException var14) {
throw new RuntimeException("API url has been modified, current url: " + this.url);
}
}
public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId, ArrayList<String> params, String sign, String extend, String ext) throws HTTPException, JSONException, IOException {
long random = SmsSenderUtil.getRandom();
long now = SmsSenderUtil.getCurrentTime();
JSONObject body = (new JSONObject()).put("tel", (new JSONObject()).put("nationcode", nationCode).put("mobile", phoneNumber)).put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now, phoneNumber)).put("tpl_id", templateId).put("params", params).put("sign", sign).put("time", now).put("extend", SmsSenderUtil.isNotEmpty(extend) ? extend : "").put("ext", SmsSenderUtil.isNotEmpty(ext) ? ext : "");
HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.url)).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random).setConnectionTimeout(60000).setRequestTimeout(60000).setBody(body.toString());
try {
HTTPResponse res = this.httpclient.fetch(req);
this.handleError(res);
return (new SmsSingleSenderResult()).parseFromHTTPResponse(res);
} catch (URISyntaxException var15) {
throw new RuntimeException("API url has been modified, current url: " + this.url);
}
}
public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId, String[] params, String sign, String extend, String ext) throws HTTPException, JSONException, IOException {
return this.sendWithParam(nationCode, phoneNumber, templateId, new ArrayList(Arrays.asList(params)), sign, extend, ext);
}
}