package com.test.client;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class SendSMS {
/**
* @param args
*/
public static void main(String[] args) {
}
/****
* 发送短信
* @param phoneNumber
* @param phonePassword
* @param sendSMSList
* @param smsContent
*/
@SuppressWarnings("unchecked")
public static void sendSMS(String phoneNumber,String phonePassword,List sendSMSList,String smsContent){
PostMethod postMethod = null;
String str="";
String CONTENT_CHARSET = "UTF-8";
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);// 设置字符,可发送中文
try {
//通过配置的用户名和密码登录飞信
postMethod = new PostMethod("http://f.10086.cn//im/login/inputpasssubmit1.action");
NameValuePair m = new NameValuePair("m",phoneNumber);
NameValuePair pass = new NameValuePair("pass",phonePassword);
postMethod.setRequestBody(new NameValuePair[] {m,pass});
client.setConnectionTimeout(10000);
client.executeMethod(postMethod);
//str = postMethod.getResponseBodyAsString();
//System.out.println("=====>" + str);
//System.out.println("服务器返回状态:" + postMethod.getStatusLine());
//登录成功后,在好友列表中搜索好友,并获得好友对应的飞信ID
System.out.println("=========搜索好友==========");
for (int i = 0; i < sendSMSList.size(); i++) {
String sendNumer = (String) sendSMSList.get(i);
String fetionID = "";//可以从数据库先查询得到
//如果数据库中手机号存在对应的飞信ID,则直接发送短信
if(fetionID!=null && fetionID.length()>0){
System.out.println("========发送消息页面==========");
PostMethod sendMsg = new PostMethod("http://f.10086.cn/im/chat/sendMsg.action?touserid="+fetionID);
NameValuePair msg = new NameValuePair("msg",smsContent);
sendMsg.setRequestBody(new NameValuePair[] {msg});
client.executeMethod(sendMsg);
}else{//否则则通过在好友列表中搜索出手机号对应的ID
System.out.println("========搜索页面==========");
postMethod = new PostMethod("http://f.10086.cn/im/index/searchOtherInfoList.action?searchText=" + sendNumer + "&searchType=friend");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
client.executeMethod(postMethod);
str = postMethod.getResponseBodyAsString();
//获得好友对应的飞信ID
Pattern patt = Pattern.compile("touserid=(.+?)&");
Matcher matcher = patt.matcher(str);
while (matcher.find()) {
fetionID = matcher.group(1);
if(fetionID!=null && fetionID.length()>0){
break;
}
}
//如果搜索的号码存在好友列表中对应的飞信ID,并发送短信
if(fetionID!=null && fetionID.length()>0){
fetionID = "";
System.out.println("========发送消息页面==========");
PostMethod sendMsg = new PostMethod("http://f.10086.cn/im/chat/sendMsg.action?touserid="+fetionID);
NameValuePair msg = new NameValuePair("msg",smsContent);
sendMsg.setRequestBody(new NameValuePair[] {msg});
client.executeMethod(sendMsg);
//并把获得的飞信号更新到数据库中,下次执行时不用再搜索查找
//…………………………
}else{//如果手机号不在好友列表中,则发送添加好友邀请
System.out.println("=========添加好友==========");
postMethod = new PostMethod("http://f.10086.cn/im/user/insertfriendsubmit.action?number=" + sendNumer + "&type=0&localName=local&nickname=name&buddylist=1");
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, CONTENT_CHARSET);
client.executeMethod(postMethod);
}
}
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
postMethod.releaseConnection();// 释放连接
}
}