秒嘀獲取簡訊驗證碼
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
public class MessageCode {
public static void main(String[] args) {
String url="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
Map<String, String> params = new HashMap<String, String>();
params.put("accountSid", "8d173a3f0231452ba5ed67ae24418064");
params.put("smsContent","【秒嘀科技】您的驗證碼是345678,30分鐘輸入有效");
// params.put("templateid", "1011709763");
params.put("param", "模板的格式為:您的訂單{1}已經處理完成,貨物即將發出,請於近{2}日內查收");
params.put("to", "15510225694");
params.put("timestamp", "20181203133030");
params.put("sig", "8de101bbe31e3f76f0c233acfe7ca15e");
params.put("respDataType", "XML");
String result=postRequest(url,params);
System.out.println(result);
}
public static String postRequest(String url, Map<String, String> params) {
//構造HttpClient的例項
HttpClient httpClient = new HttpClient();
//建立POST方法的例項
PostMethod postMethod = new PostMethod(url);
//設定請求頭資訊
postMethod.setRequestHeader("Connection", "close");
//新增引數
for (Map.Entry<String, String> entry : params.entrySet()) {
postMethod.addParameter(entry.getKey(), entry.getValue());
}
//使用系統提供的預設的恢復策略,設定請求重試處理,用的是預設的重試處理:請求三次
httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
//接收處理結果
String result = null;
try {
//執行Http Post請求
httpClient.executeMethod(postMethod);
//返回處理結果
result = postMethod.getResponseBodyAsString();
} catch (HttpException e) {
// 發生致命的異常,可能是協議不對或者返回的內容有問題
System.out.println("請檢查輸入的URL!");
e.printStackTrace();
} catch (IOException e) {
// 發生網路異常
System.out.println("發生網路異常!");
e.printStackTrace();
} finally {
//釋放連結
postMethod.releaseConnection();
}
return result;
}
}