java实现 MISC 短信业务订购
最近在做这个玩意、其中碰到的最大的问题是联络的问题,还有一个就是资料匮乏的问题。遇到问题了问移动的经理,他说他不清楚、问卓望的人他说他不是搞开发他不知道!
好了、公司申请了短信订购业务过后,我们就会得到发送订购报文的地址跟业务代码,企业代码这些报文信息,具体报文如下:
package com.bx.util;import java.io.BufferedReader;import java.io.File;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.UUID;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;/** * 移动订购业务包 * @author admin_Hzw * */public class CMMISC {/** * 反向订购请求包 * @return */public static String getRequest(String phone,String msgId){StringBuffer sendStr = new StringBuffer(); sendStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+ "<SOAP-ENV:Envelope "+"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "+"xmlns:SOAP-ENC=\"http://schemas .xmlsoap.org/soap/encoding/\" "+"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "+"xmlns=\"http://www.monternet.com/dsmp/schemas/\">"); sendStr.append("<SOAP-ENV:Header> "+"<TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\" "+"xsi:type=\"xsd:string\">"+msgId+"</TransactionID> "+" </SOAP-ENV:Header>"); sendStr.append("<SOAP-ENV:Body>"); sendStr.append("<SubscribeServiceReq xmlns=\"http://www.monternet.com/dsmp/schemas/\">"); sendStr.append("<Version>1.5.0</Version>"); sendStr.append("<MsgType>SubscribeServiceReq</MsgType>"); sendStr.append("<Send_Address>"); sendStr.append("<DeviceType>400</DeviceType>"); sendStr.append("<DeviceID>924403</DeviceID>"); sendStr.append("</Send_Address>"); sendStr.append("<Dest_Address>"); sendStr.append("<DeviceType>0</DeviceType>"); sendStr.append("<DeviceID>0028</DeviceID>"); sendStr.append("</Dest_Address>"); sendStr.append("<FeeUser_ID>"); sendStr.append("<UserIDType>1</UserIDType>"); sendStr.append("<MSISDN>"+phone+"</MSISDN>"); sendStr.append("<PseudoCode />"); sendStr.append("</FeeUser_ID>"); sendStr.append("<DestUser_ID>"); sendStr.append("<UserIDType>1</UserIDType>"); sendStr.append("<MSISDN>"+phone+"</MSISDN>"); sendStr.append("<PseudoCode />"); sendStr.append("</DestUser_ID>"); sendStr.append("<Service_ID>"); sendStr.append("<ServiceIDType>1</ServiceIDType>"); sendStr.append("<SPID>企业代码</SPID>"); sendStr.append("<SPServiceID>业务代码</SPServiceID>"); sendStr.append("<AccessNo />"); sendStr.append("<FeatureStr />"); sendStr.append("</Service_ID>"); sendStr.append("<FeatureStr />"); sendStr.append("</SubscribeServiceReq>");sendStr.append("</SOAP-ENV:Body>");sendStr.append("</SOAP-ENV:Envelope>");return sendStr.toString();}/** * 模拟返回包 * @return */public static String getResult(){StringBuffer r = new StringBuffer(); r.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("<SOAP-ENV:Envelope").append(" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"").append(" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\"").append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"").append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">").append("<SOAP-ENV:Header>").append("<TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\">9990100019175</TransactionID>").append("</SOAP-ENV:Header>").append("<SOAP-ENV:Body>").append("<SyncOrderRelationResp>").append("<MsgType>SyncOrderRelationResp</MsgType>").append("<Version>1.5.0</Version>").append("<hRet>0</hRet>").append("</SyncOrderRelationResp>").append("</SOAP-ENV:Body>").append("</SOAP-ENV:Envelope>");return r.toString();} /** * 取消包 * @param tel * @return */public static String getCanCel(String tel){StringBuffer b = new StringBuffer();b.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://www.monternet.com/dsmp/schemas/\">").append("<SOAP-ENV:Header>").append("<TransactionID xmlns=\"http://www.monternet.com/dsmp/schemas/\" xsi:type=\"xsd:string\">00240301801050</TransactionID>") .append("</SOAP-ENV:Header>").append("<SOAP-ENV:Body>").append("<UnSubscribeServiceReq xmlns=\"http://www.monternet.com/dsmp/schemas/\">").append("<Version>1.5.0</Version> ").append("<MsgType>UnSubscribeServiceReq</MsgType>") .append("<Send_Address>").append("<DeviceType>400</DeviceType>") .append("<DeviceID>924403</DeviceID>") .append("</Send_Address>").append("<Dest_Address>").append("<DeviceType>0</DeviceType>").append("<DeviceID>0028</DeviceID>") .append("</Dest_Address>").append("<FeeUser_ID>").append("<UserIDType>1</UserIDType>").append("<MSISDN>"+tel+"</MSISDN>") .append("<PseudoCode />") .append("</FeeUser_ID>").append("<DestUser_ID>").append("<UserIDType>1</UserIDType>") .append("<MSISDN>"+tel+"</MSISDN>") .append("<PseudoCode />") .append("</DestUser_ID>").append("<Service_ID>").append("<ServiceIDType>1</ServiceIDType>") .append("<SPID>企业代码</SPID>") .append("<SPServiceID>业务代码</SPServiceID>") .append("<AccessNo />") .append("<FeatureStr />") .append("</Service_ID>").append("</UnSubscribeServiceReq>").append("</SOAP-ENV:Body>").append("</SOAP-ENV:Envelope>");return b.toString();}/** * 发送报文 * @param tel 电话号码 * @param msgId 消息编号 * @return * @throws Exception */public static boolean sendMessage(String tel,String msgId) throws Exception { boolean bool = false;System.out.println("调用servlet开始================="); String sendStr = CMMISC.getRequest(tel,msgId);BufferedReader reader = null; try { String strMessage = ""; StringBuffer buffer = new StringBuffer(); URL uploadServlet = new URL("http://..ip../dsmp/dsmp.wsdl"); // 接报文的地址 HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet.openConnection(); // 设置连接参数 servletConnection.setRequestMethod("POST"); servletConnection.setDoOutput(true); servletConnection.setDoInput(true); servletConnection.setAllowUserInteraction(true); // 开启流,写入XML数据 OutputStream output = servletConnection.getOutputStream(); System.out.println("发送的报文:"); System.out.println(sendStr); output.write(sendStr.getBytes()); output.flush(); output.close(); // 获取返回的数据 InputStream inputStream = servletConnection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); while ((strMessage = reader.readLine()) != null) { buffer.append(strMessage); } System.out.println("接收返回值:" + buffer); System.out.println("--------------");//hRet等于0为成功if(Integer.valueOf(getXmlResult(buffer.toString()).get("hRet"))==0){bool = true;}System.out.println();}catch (java.net.ConnectException e) { throw new Exception(); } finally { if (reader != null) { reader.close(); } }return bool;}/** * 解析返回的XML信息 * @param xml * @return 0为成功、其它为失败 */public static Map<String,String> getXmlResult(String result) {File f = null;Map<String, String> map = new HashMap<String, String>();try{f = new File("C://temp//temp"+System.currentTimeMillis()+".xml");if(f.exists()){f.delete();} f.createNewFile();RandomAccessFile ra = new RandomAccessFile(f,"rw");ra.writeBytes(result);ra.close(); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(f); Element foo = doc.getRootElement(); List allChildren = foo.getChildren(); //获取所有节点集合for (int i = 0; i < allChildren.size(); i++) { Element e = ((Element) allChildren.get(i));String nName = e.getName();//System.out.println(nName); if(nName.equals("Header")){List hList = e.getChildren(); //获取Header节点集合for (int j = 0; j < hList.size(); j++) {Element eH = ((Element) hList.get(j));//System.out.println(eH.getName()+"---"+eH.getText()); map.put(eH.getName(), eH.getText());} }else{ List bList = e.getChildren(); //获取Body节点集合for (int j = 0; j < bList.size(); j++) {Element eB = ((Element) bList.get(j));//System.out.println(eB.getName()+"---"+eB.getContent().size()); map.put(eB.getName(), eB.getContent().size()+"");List bEList = eB.getChildren();for (int k = 0; k < bEList.size(); k++) {Element eBE = ((Element) bEList.get(k));//System.out.println(eBE.getName()+"---"+eBE.getText()); map.put(eBE.getName(), eBE.getText());} } }} }catch(Exception e){ e.printStackTrace();}finally{if(f!=null){f.delete();}}return map;}//取消public static void getCanCel() throws Exception {String xml = CMMISC.getCanCel("13985046628");BufferedReader reader = null; try { String strMessage = ""; StringBuffer buffer = new StringBuffer(); // 接报文的地址 URL uploadServlet = new URL("http://..ip../dsmp/dsmp.wsdl"); HttpURLConnection servletConnection = (HttpURLConnection) uploadServlet.openConnection(); // 设置连接参数 servletConnection.setRequestMethod("POST"); servletConnection.setDoOutput(true); servletConnection.setDoInput(true); servletConnection.setAllowUserInteraction(true); // 开启流,写入XML数据 OutputStream output = servletConnection.getOutputStream(); System.out.println("发送的报文:"); System.out.println(xml); output.write(xml.getBytes()); output.flush(); output.close(); // 获取返回的数据 InputStream inputStream = servletConnection.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream)); while ((strMessage = reader.readLine()) != null) { buffer.append(strMessage); } System.out.println("接收返回值:" + buffer); Map<String,String> map = getXmlResult(buffer.toString());System.out.println(map.get("hRet")); //0为成功}catch (java.net.ConnectException e) { throw new Exception(); } finally { if (reader != null) { reader.close(); } }}}
这是反向订购、已经测试成功了。不过反向订购太“王八”了点,报文发送过去你的手机就会收到扣费信息了......立即生效的那种。
还有记得一点就是必须要在你申请的那个ip地址对应的机器上进行报文的发送,否则就会返回“9017”的错误代码:ip非法。