最近在做這個玩意、其中碰到的最大的問題是聯絡的問題,還有一個就是資料匱乏的問題。遇到問題了問移動的經理,他說他不清楚、問卓望的人他說他不是搞開發他不知道!
好了、公司申請了簡訊訂購業務過後,我們就會得到發送訂購報文的地址跟業務代碼,企業代碼這些報文資訊,具體報文如下:
<?xml version="1.0" encoding="utf-8"?><SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Header> <TransactionID xmlns="http://www.monternet.com/dsmp/schemas/">00240301659556</TransactionID> ------該訊息編號</SOAP-ENV:Header><SOAP-ENV:Body><SyncOrderRelationReq xmlns="http://www.monternet.com/dsmp/schemas/"><Version>1.5.0</Version><MsgType>SyncOrderRelationReq</MsgType><Send_Address><DeviceType>0</DeviceType><DeviceID>0028</DeviceID></Send_Address><Dest_Address><DeviceType>400</DeviceType><DeviceID>0</DeviceID></Dest_Address><FeeUser_ID><UserIDType>1</UserIDType><MSISDN>13885078893</MSISDN> ----------------------手機號碼<PseudoCode></PseudoCode></FeeUser_ID><DestUser_ID><UserIDType>1</UserIDType><MSISDN>13885078893</MSISDN> ----------------------手機號碼<PseudoCode></PseudoCode></DestUser_ID><LinkID>SP</LinkID><ActionID>1</ActionID><ActionReasonID>1</ActionReasonID><SPID>924703</SPID> ----------------------企業代碼<SPServiceID>-PG</SPServiceID> ----------------------業務代碼<AccessMode>3</AccessMode><FeatureStr>UXI=</FeatureStr></SyncOrderRelationReq></SOAP-ENV:Body></SOAP-ENV:Envelope>
上面的是訂購的xml內容,我們需要將上面的手機號碼,企業代碼,業務代碼,訊息編號(如果你不考慮接收取消訂購的訊息的話可以不管它)替換掉,下面是我寫的一個訂購測試類別:
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://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非法。