功能描述:
发送短信工具类:该工具类定义了静态发短信的方法,所有使用的用户名,密码都需要定义为静态,这里需要通过spring 读取配置文件注入
首先定义静态变量:
private static String host="";
private static String username="";
private static String password="";
下面是静态的发送短信的方法:
/**
* @param host
* @param username
* @param password
* @param SMSBean
* @return
*/
public static String [] sendSMS(SMSBean smsBean)
{
String smsContext;
HttpMethodBase method = null;
try
{
smsContext = URLEncoder.encode(smsBean.getContent(),"utf-8");
String Url = "http://www.kasms.net/Api/smssend.aspx?user=" + username
+ "&pass=" + password + "&telno=1&msg=" + smsContext
+ "&mobiles=" + smsBean.getSendmobile();
System.out.println(Url);
HttpClient client = new HttpClient();
method = new PostMethod(Url);
method.setRequestHeader("Content-Type","text/html; charset=utf-8");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3,false));
int result = client.executeMethod(method);
if(result != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + method.getStatusLine());
return new String []{"1","连接失败"};
}
}
catch(HttpException e)
{
System.out.println("服务器异常,请检查短信发送协议是否正确!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
catch(IOException e)
{
System.out.println("IO异常,系统将自动重发3次。如一直失败!请检查网络是否正常!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
finally
{
method.releaseConnection();
}
return new String []{"0","短信发送成功"};
}
后面是通过spring注入bean的get,set方法
错误就在这里:
/**
* @return the host
*/
public static String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public static void setHost(String host)
{
SMSTools.host = host;
}
这里和平时的一看就不同,没有研究源码,应该是spring在读get,set方法时没有识别出来!
改成
/**
* @return the host
*/
public String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host)
{
SMSTools.host = host;
}
确行了...
发送短信工具类:该工具类定义了静态发短信的方法,所有使用的用户名,密码都需要定义为静态,这里需要通过spring 读取配置文件注入
首先定义静态变量:
private static String host="";
private static String username="";
private static String password="";
下面是静态的发送短信的方法:
/**
* @param host
* @param username
* @param password
* @param SMSBean
* @return
*/
public static String [] sendSMS(SMSBean smsBean)
{
String smsContext;
HttpMethodBase method = null;
try
{
smsContext = URLEncoder.encode(smsBean.getContent(),"utf-8");
String Url = "http://www.kasms.net/Api/smssend.aspx?user=" + username
+ "&pass=" + password + "&telno=1&msg=" + smsContext
+ "&mobiles=" + smsBean.getSendmobile();
System.out.println(Url);
HttpClient client = new HttpClient();
method = new PostMethod(Url);
method.setRequestHeader("Content-Type","text/html; charset=utf-8");
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3,false));
int result = client.executeMethod(method);
if(result != HttpStatus.SC_OK)
{
System.err.println("Method failed: " + method.getStatusLine());
return new String []{"1","连接失败"};
}
}
catch(HttpException e)
{
System.out.println("服务器异常,请检查短信发送协议是否正确!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
catch(IOException e)
{
System.out.println("IO异常,系统将自动重发3次。如一直失败!请检查网络是否正常!");
e.printStackTrace();
return new String []{"1","连接失败"};
}
finally
{
method.releaseConnection();
}
return new String []{"0","短信发送成功"};
}
后面是通过spring注入bean的get,set方法
错误就在这里:
/**
* @return the host
*/
public static String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public static void setHost(String host)
{
SMSTools.host = host;
}
这里和平时的一看就不同,没有研究源码,应该是spring在读get,set方法时没有识别出来!
改成
/**
* @return the host
*/
public String getHost()
{
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host)
{
SMSTools.host = host;
}
确行了...