1.使用HttpClient方式用到的jar文件:
commons-httpclient-3.0.jar
commons-codec-1.3.jar
commons-logging-1.1.1.jar
示例代码:
//短信接口url
private final static String URL="http://218.201.73.55:20332/SDKClient.asmx?wsdl";
//短信接口方法
private final static String SMSMETHOD="SmsSend";
//短信接口用户名、 密码
private final static String USERNAME="ft";
private final static String USERPWD="ft";
/**
* 通过HttpClient方式 向指定手机号码发送短信内容
* 开发步骤:
* 1、把URL的数据在浏览器访问,可以得到发送给服务器的xml数据格式
* 2、写下面的代码即可实现
* @param sendContent 发送的短信内容
* @param phones 为手机号码列表,使用半角,分开。如:13800138000,13800138001
* @param sendDateTime 定时发送时间,格式为:
2010-01-01 09:30:00
输入非日期格式字符串,则为即时发送。
* @return
* 成功:返回“1_发送号码个数”。如:“1_988”表示成功发送988个号码。
失败:返回“0_失败信息”。如:“0_用户名或密码错误”
*/
public static int smsSendByHttpClent(String sendContent,String phones,String sendDateTime){
int count=0;//发送成功的手机号码数
String soapRequestData ="<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" +
" <soap12:Body>" +
" <SmsSend xmlns=\"http://tempuri.org/\">" +
" <username>"+USERNAME+"</username>" +
" <userpwd>"+USERPWD+"</userpwd>" +
" <sendcont>"+sendContent+"</sendcont>" +
" <sendmobile>"+phones+"</sendmobile>" +
" <senddate>"+sendDateTime+"</senddate>" +
" </SmsSend>" +
" </soap12:Body>" +
"</soap12:Envelope>";
//定义一个PostMethod,这时需要指定web服务的Url
PostMethod postMethod = new PostMethod(URL);
byte[] b;
try {
b = soapRequestData.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");
//把Soap请求数据添加到PostMethod中
postMethod.setRequestEntity(re);
//生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
if(200==statusCode){
String getServerData = postMethod.getResponseBodyAsString();
count=1;
int beginIndex=getServerData.indexOf("<SmsSendResult>");
int endIndex=getServerData.indexOf("</SmsSendResult>");
count=Integer.parseInt(getServerData.substring(beginIndex, endIndex).replace("<SmsSendResult>1_", ""));
System.out.println("send success numbers:"+count+"----->"+getServerData);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return count;
}
使用XFire方式:
使用到的jar文件:
xfire-all-1.2.6.jar、XmlSchema-1.1.jar、jdom-1.0.jar、wsdl4j.jar
示例代码:
/**
* 通过XFire方式 发送短信给指定手机号码
* @param content 发送的短信内容
* @param phones 为手机号码列表,使用半角,分开。如:13800138000,13800138001
* @param date 定时发送时间,格式为:
2010-01-01 09:30:00
输入非日期格式字符串,则为即时发送
* @return
*/
public static int sendSMSCountByXFire(String content,String phones,String date){
int count=0;//发送成功的手机号码数
Client c;
try {
c = new Client(new URL(URL));
Object[] o=c.invoke(SMSMETHOD,new String[]{USERNAME,USERPWD,content,phones,date});
String str="";
for (Object oj : o) {
str=str+(String) oj;
}
count=Integer.parseInt(str.split("1_")[1]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
以上是我在做一个项目中需要调用一个短信接口所使用的两种方式,仅供大家参考.
下载示例项目(其中包含了两种方式所需要的jar文件)的链接地址: