java 调用webservice接口

来源:互联网 发布:mysql主键的唯一约束 编辑:程序博客网 时间:2024/05/02 02:31


import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;


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.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;


import com.ulink.xxx.framework.platform.config.Globals;
import com.ulink.xxxx.framework.platform.log.Logger;
import com.ulink.xxx.framework.platform.log.LoggerFactory;


/**
 * 短信消息发送接口
 * @author Administrator
 *
 */
@Service
public class SendMessage {
private static String Url = "";
private static String username="";
private static String password="";
private static Logger logger = null;

public SendMessage() {
//TODO 读取配置文件字段...
Url="http://XXXXXXXXX/webservice/sms.php?method=Submit";
username="XXXXX";
password="XXXXX";
logger = LoggerFactory.getLogger(Sendsms.class);
}
/**
* 发送短信验证码
* @param mobile 接收短信的手机号码
* @param contacts 短信模板内容
* @param objects 数据组
*/
public void send(String mobile, String content,Object[] datas){
//TODO 初始化mapjihe
Boolean isDevMode = Globals.getBooleanProperty("isDev");
if(isDevMode){
return;
}
if(mobile == null || mobile.trim().equals("")){
return;
}
 
if(content == null || content.trim().equals("")){
return;
}
HttpClient client = new HttpClient(); 
PostMethod method = new PostMethod(Url); 
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
//格式化模板
content=stringFormat(content,datas);
System.out.println(content);
NameValuePair[] data = {//提交短信
   new NameValuePair("account", username), 
   new NameValuePair("password", password), //密码可以使用明文密码或使用32位MD5加密
   //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
   new NameValuePair("mobile", mobile), 
   new NameValuePair("content", content),
};
method.setRequestBody(data);
try {
client.executeMethod(method);

String SubmitResult =method.getResponseBodyAsString();
//System.out.println(SubmitResult);


Document doc = DocumentHelper.parseText(SubmitResult); 
Element root = doc.getRootElement();
//读取返回值
String code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");
//记录返回值
logger.info(code);
logger.info(msg);
logger.info(smsid);

if("2".equals(code)){
logger.info("短信提交成功");
this.createSmsLog(mobile, content, SmsType.NOTICE, code, msg, smsid);

else{
this.createSmsLog(mobile, content, SmsType.NOTICE, code, msg, smsid);
}

} catch (HttpException e) {
logger.error(e.getMessage(), e);
this.createSmsLog(mobile, content, SmsType.NOTICE, null, "HttpException", null);
} catch (IOException e) {
logger.error(e.getMessage(), e);
this.createSmsLog(mobile, content, SmsType.NOTICE, null, "IoException", null);
} catch (DocumentException e) {
logger.error(e.getMessage(), e);
this.createSmsLog(mobile, content, SmsType.NOTICE, null, "DocumentException", null);
} catch(Exception e){
logger.error(e.getMessage(), e);
this.createSmsLog(mobile, content, SmsType.NOTICE, null, "Unknown Exception", null);
}
}

private void createSmsLog(String phone, String content, int type, String code, String msg, String smsid){
try {

} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public static String stringFormat(String str,Object[] datas){
int first=str.indexOf("{");
int next=str.indexOf("}");
String numberStart=str.substring(first+1, next);
int startIndex=0;
if(numberStart!=null&&numberStart!=""){
startIndex=Integer.parseInt(numberStart);
}
List<String>  arr=new ArrayList<String>();

for (int i=0;i<startIndex+datas.length;i++) {
if(i>=startIndex){
arr.add((String) datas[i-startIndex]);
}else {
arr.add(i+"");
}
}
str = MessageFormat.format(str, arr.toArray());
return str;
}
public static void main(String[] args) {
SendMessage send=new SendMessage();
String [] datas={"888","17:02"};
send.send("157xxxxx29", "您的验证码为{1},请于{2}内正确输入,如非本人操作,请忽略此短信。", datas);
}
}