[英]Sending SMS through low end API
我有一個形式的消息構造方法:
public static String constructMsg(CustomerInfo customer) {
... snipped
String msg = String.format("Snipped code encapsulated by customer object");
return msg;
}
API鏈接為:
http://xxx.xxx.xx.xx:8080 / bulksms?username = xxxxxxx &password = xxxx&type = 0&dlr = 1&destination = 10digitno&source = xxxxxx&message = xxxxx
在我的主要方法中,我有:
List<CustomerInfo> customer = dao.getSmsDetails(userDate);
theLogger.info("Total No : " + customer.size() );
if (!customer.isEmpty()) {
for (CustomerInfo cust : customer) {
String message = constructMsg(cust);
// Add link and '?' and query string
// use URLConnection's connect method
}
}
所以我正在使用URLConnection的connect
方法。 該API沒有任何文檔。 有什么辦法可以檢查響應?
我的另一個問題是,建議我使用ThreadPoolExecutor。 我將在這里使用它嗎?
此方法使用HTTPURLConnection執行GET請求,以字符串形式返回響應。 有很多方法可以做到這一點,雖然它不是特別出色,但確實可讀。
public String getResponse(String url, int timeout) {
HttpURLConnection c;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
default:
return "HTTP CODE: "+status;
}
} catch (MalformedURLException ex) {
Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DebugServer.class.getName()).log(Level.SEVERE, null, ex);
} finally{
if(c!=null) c.disconnect();
}
return null;
}
像這樣調用此方法:
getResponse("http://xxx.xxx.xx.xx:8080/bulksms?username=xxxxxxx&password=xxxx&type=0 &dlr=1&destination=10digitno&source=xxxxxx&message=xxxxx",2000);
我假設您網址中的空格不應存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.