联通在信业务SP反向退订

1、需求说明

搞了一段时间联通在信业务,由于某些原因,需要对一批号码进行批量退订

2、知识点说明

1、http请求,由于请求接口返回只是类似Error$000000或者OK,采用的是org.apache.commons.httpclient.HttpClient
2、3DES加密
3、BASE64加码解码

3、代码

直接上代码吧,不说废话,主要是TripleDES.java和QuxiaoBY.java两个类
不愿意看的,直接下demo吧demo下载
3.1、TripleDES.java

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

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

public class TripleDES {
    private static final Cipher cipher = initCipher();
    private static final BASE64Encoder base64 = new BASE64Encoder();

    private static final Cipher initCipher() {
        try {
            // 添加新安全算法:PKCS7
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            String algorithm = "DESede/ECB/PKCS7Padding";
            SecretKey desKey = new SecretKeySpec(
                    (new BASE64Decoder())
                            .decodeBuffer(Common.DES_KEY_STRING),
                    algorithm);
            Cipher tcipher = Cipher.getInstance(algorithm);
            tcipher.init(Cipher.ENCRYPT_MODE, desKey);
            return tcipher;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt1(String src) {
        return base64.encode(encrypt(src.getBytes()));
    }

    public static byte[] encrypt(byte[] src) {
        try {
            return cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) throws Exception {
        System.out.println("1-->"+encrypt1("12345"));

    }

}

3.2、QuxiaoBY.java

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class QuxiaoBY {
    public static void selectbyhaoma() throws SQLException, IOException {

        List byhmList = DbUtil.selectAll("select phone,accesstime from t_byhaoma");
        //System.out.println("list:" + byhmList.get(0));

        for (int i = 0; i < byhmList.size(); i++) {
            HashMap<String, Object> hm = (HashMap<String, Object>) byhmList
                    .get(i);
            System.out.println(String.valueOf(i)+"-->>"+hm.get("phone") + "-->>"
                    + requesturl(hm));
        }
    }

    public static String desString(HashMap<String, Object> hashMap) throws UnsupportedEncodingException {
        String orignalString =Common.SPNUMBER_STRING + "$" + hashMap.get("phone")
                + "$" + Common.SERVICE_TAG_STRING + "$" + hashMap.get("accesstime");
        String EncodeStr = URLEncoder.encode(TripleDES
                .encrypt1((orignalString)),"UTF-8");
        //System.out.println(orignalString+"<<<<>>>"+EncodeStr);
        return EncodeStr;
    }

    public static String requesturl(HashMap<String, Object> hashMap) throws UnsupportedEncodingException {

        String strurl = Common.TUIDING_URL + "SpNumber="+Common.SPNUMBER_STRING+"&AccessTime="+((String)hashMap.get("accesstime")).replace(" ", "%20")+"&EncodeStr="+desString(hashMap);
        String resultString = "null";
        HttpClient httpClient = new HttpClient();
        // 创建GET方法的实例
        GetMethod getMethod = new GetMethod(strurl);
        // 使用系统提供的默认的恢复策略
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        try {
            // 执行getMethod
            int statusCode = httpClient.executeMethod(getMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: "
                        + getMethod.getStatusLine());
            }
            // 读取内容
            byte[] responseBody = getMethod.getResponseBody();
            // 处理内容
            resultString =  new String(responseBody);
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            e.printStackTrace();
        } finally {
            // 释放连接
            getMethod.releaseConnection();
        }
        return resultString;
    }

    public static void main(String[] args) throws IOException, SQLException,
            ParseException {

        selectbyhaoma();
        System.out.println("end");
    }
}

3.3、DbUtil.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyq
 * @created 2016-12-18
 */
public class DbUtil {

    public static List selectAll(String sql) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            Class.forName(Common.DRIVER);
            conn = DriverManager.getConnection(Common.URL, Common.USERNAME,
                    Common.PASSWORD);
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            if (rs == null)   
                   return Collections.EMPTY_LIST;   
               ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等   
               int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数   
               List list = new ArrayList();   
               Map rowData = new HashMap();   
               while (rs.next()) {   
                rowData = new HashMap(columnCount);   
                for (int i = 1; i <= columnCount; i++) {   
                        rowData.put(md.getColumnName(i), rs.getObject(i));   
                }   
                list.add(rowData);   
               }   
               return list;   
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
        return null;
    }

}

3.4、Common.java

/**
 * @author zyq
 * @created 2016-12-18
 */
public class Common {

    // connect the database
    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String DB_NAME = "test";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "12345";
    public static final String IP = "127.0.0.1";
    public static final String PORT = "3306";
    public static final String URL = "jdbc:mysql://" + IP + ":" + PORT + "/" + DB_NAME+"?characterEncoding=utf-8";

    public static final String TUIDING_URL = "http://www.tuiding.com?";//请求地址
    public static final String SPNUMBER_STRING ="10650000";//spnumber
    public static final String SERVICE_TAG_STRING = "xxxx";//指令
    public static final String DES_KEY_STRING = "xxxx";//密钥
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值