📄 csofthttpclientsmsservice.java
字号:
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.BufferedReader;
import java.util.Calendar;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author philip gleghorn <pgleghorn at fatwire.com>
*
*/
public class CSoftHttpClientSMSService extends SMSService {
private HttpClient client;
private String mmsServiceUrl = "https://www.csoft.co.uk/sendmms";
private String smsServiceUrl = "https://www.csoft.co.uk/sendsms";
private String username = "username.123456";
private String PIN = "12345678";
private static Log log = LogFactory.getLog(CSoftHttpClientSMSService.class
public CSoftHttpClientSMSService() {
super();
client = new HttpClient();
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#getAccountMessageLimit()
*/
public int getAccountMessageLimit() {
log.debug("in getMessageLimit");
PostMethod method = new PostMethod(smsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("AvailableMessages", "");
String result = new String();
try {
result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
String[] retArray = result.split("=");
return Integer.valueOf(retArray[1]);
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#getAccountBalanceCurrency()
*/
public String getAccountBalanceCurrency() {
log.debug("in getAccountBalanceCurrency");
PostMethod method = new PostMethod(smsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("AvailableCredit", "");
String result = new String();
try {
result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
String[] retArray = result.split("=");
String[] r2 = retArray[1].split(" ");
return r2[0];
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#getAccountBalance()
*/
public float getAccountBalance() {
log.debug("in getAccountBalance");
PostMethod method = new PostMethod(smsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("AvailableCredit", "");
String result = new String();
try {
result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
String[] retArray = result.split(" ");
return Float.valueOf(retArray[1]);
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#sendSimpleSMS(java.lang.String,
* java.lang.String)
*/
public void sendSimpleSMS(String msgTo, String msgBody) {
log.debug("in sendSimpleSMS");
PostMethod method = new PostMethod(smsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("SendTo", msgTo);
method.setParameter("Message", msgBody);
try {
String result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#sendFlashSMS(java.lang.String,
* java.lang.String)
*/
public void sendFlashSMS(String msgTo, String msgBody) {
log.debug("in sendFlashSMS");
PostMethod method = new PostMethod(smsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("SendTo", msgTo);
method.setParameter("FlashMessage", msgBody);
try {
String result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
}
/*
* (non-Javadoc)
*
* @see com.ipanema.support.sms.SMSService#sendWAPPushSMS(java.lang.String,
* java.lang.String, java.lang.String)
*/
public void sendWAPPushSMS(String msgTo, String msgUrl, String msgTitle) {
log.debug("in sendWAPPushSMS");
PostMethod method = new PostMethod(mmsServiceUrl);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
method.setParameter("Username", username);
method.setParameter("PIN", PIN);
method.setParameter("SendTo", msgTo);
method.setParameter("si-href", msgUrl);
method.setParameter("si-content", msgTitle);
// set a unique id for each message
method.setParameter("si-id", String.valueOf(Calendar.getInstance()
.getTimeInMillis()));
String result = new String();
try {
result = doHttpServiceRequest(method);
log.debug("result is: " + result);
} catch (Exception e) {
log.warn(e.toString());
}
}
/**
* Executes a HttpClient PostMethod and returns the entire response as
* String.
*
* TODO: Proper handling of the error codes returned from the
* service. See http://www.csoft.co.uk/nox8/report_number.pl
*
* @param method
* A HttpClient PostMethod with the required arguments
* @return The entire response
* @throws HttpException
* @throws IOException
*/
private String doHttpServiceRequest(PostMethod method)
throws HttpException, IOException {
log.debug("in doHttpServiceRequest");
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.info("Method failed: " + method.getStatusLine());
}
InputStream is = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
StringBuffer response = new StringBuffer();
while ((line = br.readLine()) != null) {
response.append(line);
}
method.releaseConnection();
return response.toString();
}
public static void main(String args[]) {
log.debug("in main");
SMSService sms = new CSoftHttpClientSMSService();
log.info(sms.getAccountBalanceCurrency());
log.info(sms.getAccountBalance());
int limitBefore = sms.getAccountMessageLimit();
log.info(limitBefore);
// sms.sendWAPPushSMS("+316xxxxxxxx", "http://www.google.com", "subject");
// sms.sendSimpleSMS("+316xxxxxxxx", "this is a simple sms. www.google.com www.yahoo.co.uk");
// sms.sendFlashSMS("+316xxxxxxxx", "this is a flash message. www.google.com www.yahoo.co.uk");
int limitAfter = sms.getAccountMessageLimit();
log.info("operation took " + (limitBefore - limitAfter) + " units");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -