Wap Push的各类代码

C++

/*
* gcc x.cpp -I/usr/include -I/usr/local/include -L/usr/local/lib -liconv
*/

#include <stdio.h>
#include <string.h>
#include <iconv.h>


#define MAX_BUF_SIZE 4096

const char* fromcode = "GBK";
const char* tocode = "UTF-8";

void getPushURL(const char *linkname, const char *linkurl, char *fURL);

int main(int argc, char *argv[])
{
   char pURL[1024] = "";
    char *linkname = "雅讯天地";
    char *linkurl = "www.easyhappy.com";
   getPushURL(linkname,linkurl,pURL);
    printf("%s,%s,%s/n",linkname,linkurl,pURL);
return 0;
}



void getPushURL(const char *linkname, const char *linkurl, char *fURL)
{
iconv_t cd;
char url[MAX_BUF_SIZE] = "";
char inbuf[MAX_BUF_SIZE] = "";
char outbuf[MAX_BUF_SIZE] = "";
unsigned char pushURL[MAX_BUF_SIZE] = "";
const char* inptr = inbuf;
char* outptr = outbuf;
    size_t insize;
    size_t outsize = sizeof(outbuf);
int msglen = 0;
int i,pos = 0;

strncpy(inbuf, linkname, MAX_BUF_SIZE);
strncpy(url, linkurl, MAX_BUF_SIZE);

cd = iconv_open(tocode, fromcode);
insize = strlen(inbuf);
iconv(cd, &inptr, &insize, &outptr, &outsize);
iconv_close(cd);
msglen = MAX_BUF_SIZE - outsize;

i = 0;
pushURL[i++] = 0x06;
pushURL[i++] = 0x05;
pushURL[i++] = 0x04;
pushURL[i++] = 0x0b;
pushURL[i++] = 0x84;
pushURL[i++] = 0x23;
pushURL[i++] = 0xf0;
pushURL[i++] = 0x72;
pushURL[i++] = 0x06;
pushURL[i++] = 0x0a;
pushURL[i++] = 0x03;
pushURL[i++] = 0xae;
pushURL[i++] = 0x81;
pushURL[i++] = 0xea;
pushURL[i++] = 0xaf;
pushURL[i++] = 0x82;
pushURL[i++] = 0x8d;
pushURL[i++] = 0xae;

pos = i;
pushURL[i++] = 159;
pushURL[i++] = 0x87;
pushURL[i++] = 0x01;
pushURL[i++] = 0x05;
pushURL[i++] = 0x6a;
pushURL[i++] = msglen+1;
memcpy(pushURL+i, outbuf, msglen);
i+= msglen;
pushURL[i++] = 0x00;
pushURL[i++] = 0x45;
pushURL[i++] = 0xc6;
pushURL[i++] = 0x0c;
pushURL[i++] = 0x03;
memcpy(pushURL+i, url, strlen(url));
i += strlen(url);
pushURL[pos] += strlen(url);
pushURL[i++] = 0x00;
pushURL[i++] = 0x08;
pushURL[i++] = 0x01;
pushURL[i++] = 0x83;
pushURL[i++] = 0x00;
pushURL[i++] = 0x01;
pushURL[i++] = 0x01;


for (int j=0;j<i;j++)
{
sprintf(fURL, "%s%02X", fURL,pushURL[j]);
}
}

 

Java代码
package com.infusio.jds.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class SMSUtil {
       
        public final static int GB_TEXT = 16;
        public final static int UTF8_TEXT = 17;
       

        private final String version = "200";

        private String customer;

        private String pwd;

        private String srcmobile;

        private String destmobile;

        private String source;

        private String service;

        private String type = "16";

        private final String pid = "0";

        private String udhi = "0";

        private final String msgid = "0";

        private final String time = "0";

        private String message;

        public void setService(String svid) {
                this.service = svid;
        }

        public void setCustomer(String cust) {
                this.customer = cust;
        }

        public void setPwd(String pwd) {
                this.pwd = pwd;
        }

        public void setSrcMobile(String src) {
                this.srcmobile = src;
        }

        public void setDestMobile(String dest) {
                this.destmobile = dest;
        }

        public void setShortCode(String code) {
                this.source = code;
        }

        public void setMsg(String msg) {
                this.message = msg;
        }

        private String buildPostStream() throws Exception {
                Element root = new Element("linkrich-mt");

                root.addContent(new Element("version").addContent(version));
                root.addContent(new Element("customer").addContent(customer));
                root.addContent(new Element("pwd").addContent(pwd));
                root.addContent(new Element("service").addContent(service));
                root.addContent(new Element("srcmobile").addContent(srcmobile));
                root.addContent(new Element("source").addContent(source));
                root.addContent(new Element("destmobile").addContent(destmobile));
                root.addContent(new Element("type").addContent(type));
                root.addContent(new Element("message").addContent(message));
                root.addContent(new Element("pid").addContent(pid));
                root.addContent(new Element("udhi").addContent(udhi));

                root.addContent(new Element("time").addContent(time));
//                XMLOutputter out = new XMLOutputter("", true, "UTF-8");
                XMLOutputter out = new XMLOutputter("", true, "GBK");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                out.output(root, baos);
                return baos.toString();
                // String result =
                // "<linkrich-mt><version>200</version><customer>CP360</customer><pwd>360CP</pwd><service>0360A0015I</service><srcmobile>13002102397</srcmobile><source>9002360</source><destmobile>13002102397</destmobile><type>16</type><pid>0</pid><udhi>0</udhi><msgid>1960430572</msgid><time>20051104105542</time><message>From
                // Netsize</message></linkrich-mt>";
                //
                // return result;
        }

        public String sendSMS() throws Exception {

                String content = buildPostStream();

                URL url = new URL("http", "111.111.111.111", 16002, "/");
                // URL url = new URL("http", "211.157.121.51", 6951, "/");

                HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();

                httpconn.setRequestMethod("POST");
                httpconn.setDoOutput(true);
                httpconn.setDoInput(true);
                httpconn.setRequestProperty("Content-Length", Integer.toString(content.length()));
                // httpconn.setRequestProperty("Content-Type", "text/xml; charset=gb2312");
                httpconn.setRequestProperty("Content-Type", "application/xml");
                httpconn.setRequestProperty("Host", "111.111.11.111");
                httpconn.setRequestProperty("Connection", "Keep-Alive");
                PrintWriter printwriter = new PrintWriter(httpconn.getOutputStream());
               
                printwriter.println(content);
                printwriter.println();

                printwriter.flush();

                printwriter.close();

                httpconn.disconnect();
                System.out.println("===== send sms content === begin ===== ");
                System.out.println(  content );
                System.out.println("===== send sms content === end ===== ");
               
                int i;

                InputStream instream = httpconn.getInputStream();

                StringBuffer sb = new StringBuffer();

                while((i = instream.read()) > -1) {
                        sb.append((char)i);
                }
                instream.close();
                return sb.toString();

        }

        public void setUdhi(String udhi) {
                this.udhi = udhi;
        }

        public void setType(String type) {
                this.type = type;
        }

        /**
         * 得到BASE 64编码的字符串
         *
         * @param s
         *            要转换的字符
         * @return 转换后的字符
         */
        public static String getBASE64(String s) {
                if(s == null)
                        return null;
                return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
        }

        /**
         * 反向转换BASE 64编码
         *
         * @param s
         *            BASE 64编码的字符
         * @return BASE 64编码前的字符
         */
        public static String getFromBASE64(String s) {
                if(s == null)
                        return null;
                BASE64Decoder decoder = new BASE64Decoder();
                try {
                        byte[] b = decoder.decodeBuffer(s);
                        System.out.print(new String(b));
                        return new String(b);
                }
                catch(Exception e) {
                        return null;
                }
        }

        /**
         * 产生PUSH二进制
         *
         * @param content
         *            PUSH标题
         * @param url
         *            String PUSH地址
         * @return byte[] PUSH二进制
         */
        public static byte[] format(String content, String url) {

                byte[] bytContent = null;
                byte[] bytURL = url.getBytes();
                try {
                        bytContent = content.getBytes();
//                        content = new String(bytContent, "UTF-8");
//                        bytContent = content.getBytes("UTF-8");
                }
                catch(Exception ex) {
                        ex.printStackTrace();
                }

                int iContentLen = bytContent.length;
                int iURLLen = bytURL.length;

                byte[] bytMsg = new byte[36 + iContentLen + iURLLen];

                int i = 0;
                bytMsg[i++] = (byte)0x06;
                bytMsg[i++] = (byte)0x05;
                bytMsg[i++] = (byte)0x04;
                bytMsg[i++] = (byte)0x0b;
                bytMsg[i++] = (byte)0x84;
                bytMsg[i++] = (byte)0x23;
                bytMsg[i++] = (byte)0xf0;
                bytMsg[i++] = (byte)0x72;
                bytMsg[i++] = (byte)0x06;
                bytMsg[i++] = (byte)0x0a;
                bytMsg[i++] = (byte)0x03;
                bytMsg[i++] = (byte)0xae;
                bytMsg[i++] = (byte)0x81;
                bytMsg[i++] = (byte)0xea;
                bytMsg[i++] = (byte)0xaf;
                bytMsg[i++] = (byte)0x82;
                bytMsg[i++] = (byte)0x8d;
                bytMsg[i++] = (byte)0xae; // 18

                int ipos = i;
                bytMsg[i++] = (byte)159;
                bytMsg[i++] = (byte)0x87;
                bytMsg[i++] = (byte)0x01;
                bytMsg[i++] = (byte)0x05;
                bytMsg[i++] = (byte)0x6a;
                bytMsg[i++] = (byte)(iContentLen + 1);

                System.arraycopy(bytContent, 0, bytMsg, i, iContentLen);
                i += iContentLen;
                bytMsg[i++] = (byte)0x00;
                bytMsg[i++] = (byte)0x45;
                bytMsg[i++] = (byte)0xc6;
                bytMsg[i++] = (byte)0x0c;
                bytMsg[i++] = (byte)0x03;

                System.arraycopy(bytURL, 0, bytMsg, i, iURLLen);
                i += iURLLen;
                bytMsg[ipos] += (byte)iURLLen;
                bytMsg[i++] = (byte)0x00;
                bytMsg[i++] = (byte)0x08;
                bytMsg[i++] = (byte)0x01;
                bytMsg[i++] = (byte)0x83;
                bytMsg[i++] = (byte)0x00;
                bytMsg[i++] = (byte)0x01;
                bytMsg[i++] = (byte)0x01;

                // System.out.println("BYTE LENGTH : " + bytMsg.length);

                return bytMsg;
        }

        /**
         * 发送PUSH信息
         *
         * @param mobileNumber
         *            要接收PUSH信息的号码
         * @param subject
         *            PUSH标题
         * @param url
         *            PUSH地址
         * @throws Exception
         *             异常
         * @return 发送结果 0 - 正常 1 - 错误
         */

        private static String stringify(byte buf[]) {
                StringBuffer sb = new StringBuffer(2 * buf.length);
                for(int i = 0; i < buf.length; i++) {
                        int h = (buf[ i ] & 0xf0) >> 4;
                        int l = (buf[ i ] & 0x0f);
                        sb.append(new Character((char)((h > 9) ? 'a' + h - 10 : '0' + h)));
                        sb.append(new Character((char)((l > 9) ? 'a' + l - 10 : '0' + l)));
                }
                return sb.toString();
        }
 public static int sendPush(String mobileNumber, String subject, String url) throws Exception {

                int resultID = 0;

                SMSUtil controller = new SMSUtil();

                BASE64Encoder coder = new BASE64Encoder();

                controller.setType("161");
                controller.setUdhi("1");

                controller.setDestMobile(mobileNumber);
                controller.setSrcMobile("13601248557");
                controller.setMsg(coder.encode(format(subject, url)));
                //controller.setMsg(stringify(format(subject, url)));

                controller.setPwd("23232");
                controller.setService("3213232");
                controller.setShortCode("232323");
                controller.setCustomer("23232");

                // File f = new File("d:/black.txt");
                //
                // BufferedReader br = new BufferedReader(new FileReader(f));
                //
                // String line = null;
                //
                // while((line = br.readLine()) != null) {
                //
                // if(mobileNumber.equals(UtilTools.nullToSpace(line))) {
                // return 3;//用户在黑名单
                // }
                //
                // }
                //
                // br.close();

                String responseStr = controller.sendSMS();
                System.out.println("=============" + responseStr);
                if(responseStr.indexOf("<status>0</status>") < 0) { // 失败
                        resultID = Integer.parseInt(responseStr.substring(responseStr.indexOf("<status>") + 8, responseStr.indexOf("</status>")));
                }
                return resultID;
        }

        /**
         * 发送短信
         *
         * @param mobileNumber
         *            要接收短信的号码
         * @param msgContent
         *            发送短信内容
         * @throws Exception
         *             抛出异常
         * @return int 发送结果 0 - 正常 1 - 错误
         */
        public static int sendSMS(String mobileNumber, String msgContent) throws Exception {
                int resultID = 0;
                SMSUtil controller = new SMSUtil();
                // BASE64Encoder coder = new BASE64Encoder();
                controller.setType("16");
                controller.setUdhi("0");
                controller.setCustomer("12222");
                controller.setDestMobile(mobileNumber);
                controller.setSrcMobile("13800000000");
                controller.setMsg(msgContent);
                controller.setPwd("21211");
//                controller.setService("212121212");
                controller.setService("1212121212");
                controller.setShortCode("1212121");
               

                // File f = new File("d:/black.txt");
                //
                // BufferedReader br = new BufferedReader(new FileReader(f));
                //
                // String line = null;
                //
                // while((line = br.readLine()) != null) {
                //
                // if(mobileNumber.equals(UtilTools.nullToSpace(line))) {
                // return 3;//用户在黑名单
                // }
                //
                // }

                // br.close();
                String responseStr = controller.sendSMS();
                if(responseStr.indexOf("<status>0</status>") < 0) { // 失败
                        resultID = 1;
                }
                return resultID;

        }
        public static int sendSMS(String mobileNumber, String msgContent, int messageFormat ) throws Exception {
                int resultID = 0;
                SMSUtil controller = new SMSUtil();
                // BASE64Encoder coder = new BASE64Encoder();
                if( GB_TEXT == messageFormat )
                        controller.setType("16");
                if( UTF8_TEXT == messageFormat )
                        controller.setType("17");
                controller.setUdhi("0");
                controller.setCustomer("321312");
                controller.setDestMobile(mobileNumber);
                controller.setSrcMobile("13800000000");
                controller.setMsg(msgContent);
                controller.setPwd("2121");
//                controller.setService("2312321321");
                controller.setService("2132321321");
                controller.setShortCode("31221");
               

                // File f = new File("d:/black.txt");
                //
                // BufferedReader br = new BufferedReader(new FileReader(f));
                //
                // String line = null;
                //
                // while((line = br.readLine()) != null) {
                //
                // if(mobileNumber.equals(UtilTools.nullToSpace(line))) {
                // return 3;//用户在黑名单
                // }
                //
                // }

                // br.close();
                String responseStr = controller.sendSMS();
                if(responseStr.indexOf("<status>0</status>") < 0) { // 失败
                        resultID = 1;
                }
                return resultID;

        }
}
C#片断

/**
         * 产生PUSH二进制
         *
         * @param content
         *            PUSH标题
         * @param url
         *            String PUSH地址
         * @return byte[] PUSH二进制
         */
                public static string smsWapPushFormat(String content, String url)
                {
                        Encoding encodingUTF = System.Text.Encoding.UTF8;
                        byte[] bytContent = encodingUTF.GetBytes(content);
                        byte[] bytURL = encodingUTF.GetBytes(url);

                        int iContentLen = bytContent.Length;
                        int iURLLen = bytURL.Length;

                        byte[] bytMsg = new byte[36 + iContentLen + iURLLen];

                        int i = 0;
                        bytMsg[i++] = (byte) 0x06;
                        bytMsg[i++] = (byte) 0x05;
                        bytMsg[i++] = (byte) 0x04;
                        bytMsg[i++] = (byte) 0x0b;
                        bytMsg[i++] = (byte) 0x84;
                        bytMsg[i++] = (byte) 0x23;
                        bytMsg[i++] = (byte) 0xf0;
                        bytMsg[i++] = (byte) 0x72;
                        bytMsg[i++] = (byte) 0x06;
                        bytMsg[i++] = (byte) 0x0a;
                        bytMsg[i++] = (byte) 0x03;
                        bytMsg[i++] = (byte) 0xae;
                        bytMsg[i++] = (byte) 0x81;
                        bytMsg[i++] = (byte) 0xea;
                        bytMsg[i++] = (byte) 0xaf;
                        bytMsg[i++] = (byte) 0x82;
                        bytMsg[i++] = (byte) 0x8d;
                        bytMsg[i++] = (byte) 0xae; // 18

                        int ipos = i;
                        bytMsg[i++] = (byte) 159;
                        bytMsg[i++] = (byte) 0x87;
                        bytMsg[i++] = (byte) 0x01;
                        bytMsg[i++] = (byte) 0x05;
                        bytMsg[i++] = (byte) 0x6a;
                        bytMsg[i++] = (byte) (iContentLen + 1);

                        Array.Copy(bytContent,0,bytMsg,i,iContentLen);
                        //System.arraycopy(bytContent, 0, bytMsg, i, iContentLen);
                        i += iContentLen;
                        bytMsg[i++] = (byte) 0x00;
                        bytMsg[i++] = (byte) 0x45;
                        bytMsg[i++] = (byte) 0xc6;
                        bytMsg[i++] = (byte) 0x0c;
                        bytMsg[i++] = (byte) 0x03;

                        Array.Copy(bytURL,0,bytMsg,i,iURLLen);
                        //System.arraycopy(bytURL, 0, bytMsg, i, iURLLen);
                        i += iURLLen;
                        bytMsg[ipos] += (byte) iURLLen;
                        bytMsg[i++] = (byte) 0x00;
                        bytMsg[i++] = (byte) 0x08;
                        bytMsg[i++] = (byte) 0x01;
                        bytMsg[i++] = (byte) 0x83;
                        bytMsg[i++] = (byte) 0x00;
                        bytMsg[i++] = (byte) 0x01;
                        bytMsg[i++] = (byte) 0x01;

                        // System.out.println("BYTE LENGTH : " + bytMsg.length);

                        string s = null;
                        for (int j =0;j < bytMsg.Length;j++)
                        {
                                s += BitConverter.ToString(bytMsg,j,1);
                        }
                        s = String.Format("{0:X2}",s);
                               
                        return s;
                }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值