天天看點

HttpKit 基于Jfianl 調用第三方接口案例

public class LanxunService {

    private Logger logger = Logger.getLogger(getClass());

    @Inject
    TblcodemstDao codemstDao;

    public boolean uploadFile(String item_id, String filename) throws Exception {
        // 源位址路徑
        Tblcodemst code = codemstDao.selectByTypeNoDel("SOURCE_PLAYBACK_VIDEO_PATH");
        String source_path;
        if (code.content1.endsWith("/")) {
            source_path = code.content1 + filename;
        } else {
            source_path = code.content1 + "/" + filename;
        }
        // 釋出播放位址路徑
        code = codemstDao.selectByTypeNoDel("PUBLISH_PLAYBACK_VIDEO_PATH");
        String publish_path;
        if (code.content1.endsWith("/")) {
            publish_path = code.content1 + filename;
        } else {
            publish_path = code.content1 + "/" + filename;
        }

        // 藍汛使用者名
        code = codemstDao.selectByTypeAndSortNoDel("LANXUN_INFO", 0);
        String cust_id = code.content2;

        // 藍汛密碼
        code = codemstDao.selectByTypeAndSortNoDel("LANXUN_INFO", 1);
        String passwd = code.content2;

        // 對稱加密Key
        code = codemstDao.selectByTypeAndSortNoDel("LANXUN_INFO", 2);
        String deckey = code.content2;

        try {
            cust_id = DesUtil.Decrypt(cust_id, deckey);
            passwd = DesUtil.Decrypt(passwd, deckey);
        } catch (Exception ex) {
            logger.error("藍訊資訊對稱解密時發生系統異常", ex);
            throw ex;
        }

        String md5 = CryptUtil.getMD5(item_id + cust_id + "chinacache" + passwd);
        String data = "op=publish&context=<?xml version=\"1.0\" encoding=\"UTF-8\"?><ccsc><cust_id>" + cust_id
                + "</cust_id><passwd>" + md5 + "</passwd><item_id value=\"" + item_id + "\"><source_path>" + source_path
                + "</source_path><publish_path>" + publish_path + "</publish_path></item_id></ccsc>";
        String result = HttpKit.post("http://vbu.fds.ccgslb.net:8080/fds/soap/receiveTask.php", data);
        if (StringUtils.isEmpty(result)) {
            return false;
        }

        result = StringUtils.substringBetween(result, "<result>", "</result>");
        if (!"SUCCESS".equals(result)) {
            return false;
        }
        return true;
    }
}


           
=============================
回調函數:
public void getXmlResult() {
        String result = "FAILURE";
        Map<String, String[]> requestParams = getRequest().getParameterMap();
        logger.debug("藍訊異步回調:");
        logger.debug(requestParams);
        String xml = requestParams.get("context")[0];
        String status = StringUtils.substringBetween(xml, "<op_status>", "</op_status>");
        Short delFlg = 3;
        switch (status) {
        case "download failed":
            delFlg = 3;
            break;
        case "download finish":
            delFlg = 4;
            break;
        case "sync finish":
            delFlg = 0;
            break;
        default:
            break;
        }

        String item_id = StringUtils.substringBetween(xml, "<item_id value=\"", "\">");

        Tblplayback playback = playbackDao.lockById(item_id);
        if (playback != null) {
            playback.delFlg = delFlg;
            playback.updDat = utilDao.getCurTimestamp();
            playback.updUsrId = "LanXun";
            int res = playbackDao.update(playback);
            if (res > 0) {
                result = "SUCCESS";
            }
        }

        renderText("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><ccsc><result>" + result + "</result><detail></detail></ccsc>");
    }
++++++++++++++++++++++++++++++ 

           
包含heard :

public static JSONObject messageHistory(String date) {
        StringBuilder data = new StringBuilder();
        data.append("date=").append(date);

        System.out.println("messageHistory data:" + data);
        String json = HttpKit.post(URL_MESSAGE_HISTORY, data.toString(), getHeaders());
        System.out.println("messageHistory result:" + json);
        JSONObject result = JSONObject.parseObject(json);
        if (!"200".equals(result.getString("code"))) {
            throw new ServiceException((int) result.get("code"), "『融雲』查詢聊天室消息失敗");
        }
        return result;
    }

    private static Map<String, String> getHeaders() {
        String nonce = String.valueOf((int) (Math.random() * 1000000));
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        StringBuilder toSign = new StringBuilder(APP_SECRET).append(nonce).append(timestamp);
        String sign = CryptUtil.SHA1(toSign.toString());

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("RC-App-Key", APP_KEY);
        headers.put("RC-Nonce", nonce);
        headers.put("RC-Timestamp", timestamp);
        headers.put("RC-Signature", sign);
        return headers;
    }
           

繼續閱讀