package ce.cell.message.pool; import java.io.IOException; import java.net.URLEncoder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.params.HttpMethodParams; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * 短信发送 * @author needytwh * */ public class MessageUtils { private MessageUtils(){}; private static final String sendUrl="http://112.74.139.4:8002/sms3_api/xmlapi/send.jsp"; private static final String statusUrl="http://112.74.139.4:8002/sms3_api/xmlapi/report.jsp"; private static final String upUrl="http://112.74.139.4:8002/sms3_api/xmlapi/upmsg.jsp"; public static String sendMsg(String userid,String password,String phone,String content) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); rootElement.addAttribute("password", password); Element submitElement = rootElement.addElement("submit"); submitElement.addAttribute("phone", phone); submitElement.addAttribute("content", content); String result = submitUrl(createDocument,sendUrl); return result; } private static String submitUrl(Document createDocument,String url) throws IOException, HttpException { HttpClient httpClient = new HttpClient(); PostMethod method = new PostMethod(url); method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000); RequestEntity requestEntity=new StringRequestEntity(createDocument.asXML()); method.setRequestEntity(requestEntity); httpClient.executeMethod(method); String result = method.getResponseBodyAsString(); return result; } public static String getMsgStatus(String userid,String password) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); rootElement.addAttribute("password", password); String result = submitUrl(createDocument,statusUrl); return result; } public static String getMsgUp(String userid,String password) throws HttpException, IOException{ Document createDocument = DocumentHelper.createDocument(); Element rootElement = DocumentHelper.createElement("root"); createDocument.setRootElement(rootElement); rootElement.addAttribute("userid", userid); rootElement.addAttribute("password", password); String result = submitUrl(createDocument,upUrl); return result; } public static MessageUtils getInstance() { return new MessageUtils(); } }