第三方接口 CallNetApi (自己写connection)

  public NetResultMap getTicketByProps(String msisdn, String ticketId,
        Map<String, Object> result, int apiFailStatus) throws Exception {
        NetResultMap mapUsrInfo = new NetResultMap();
        StringBuilder sbXmlParam = new StringBuilder();
        String url = getProperty("apiUrlIServer")+ getProperty("getTicketByPropsPostUrl");
        String sendXml = "";
        sbXmlParam.append("<Request>");
        sbXmlParam.append("<msisdn>" + msisdn + "</msisdn>");
        sbXmlParam.append("<ticketId>" + ticketId + "</ticketId>");
        sbXmlParam.append("</Request>");
        sendXml = sbXmlParam.toString();

        CallNetApi callNetApi = new CallNetApi();
        NetResult rst = callNetApi.Post(url, sendXml);
        if (rst.status != 0) {
            result.put("status", apiFailStatus);
            result.put("resultCode", rst.status);
            return null;
        }

        mapUsrInfo.map = readGetTicketByPropsResult(rst.response);
        if (mapUsrInfo.containsKey("resultCode")) {
            if ((mapUsrInfo.get("resultCode").equals("200") == false)) {
                result.put("status", apiFailStatus);
                result.put("resultCode", mapUsrInfo.get("resultCode"));
                return null;
            }
        } else {
            logger.error("兑换书券接口返回格式不正");
            result.put("status", 9);
            return null;
        }

        return mapUsrInfo;
    }

public class CallNetApi {
    protected Logger logger = Logger.getLogger(getClass());
    
    public NetResult Post(String postbackUrl, String requestXml) throws Exception {
        NetResult rst =  new NetResult();
        rst.response="";
        rst.status=0;
        Boolean isFist =true;
        StringBuilder sb = new StringBuilder();

        try {
            logger.info(postbackUrl);
            URL url = new URL(postbackUrl);

            // 设定连接的相关参数
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            OutputStreamWriter out = new OutputStreamWriter(
                    connection.getOutputStream(), "UTF-8");

            logger.info(requestXml);
            out.write(requestXml);            
            out.flush();
            out.close();
            String strLine = "";

            String strResponse = "";
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"));
            while ((strLine = reader.readLine()) != null) {
                strResponse += strLine + "\n";
            }
            rst.response = strResponse;
            logger.info(strResponse);
        } catch (MalformedURLException ex) {
            throw ex;
        } catch (Exception e) {
            if(e.getMessage().startsWith("Server returned HTTP response code: ")){
                String tmp = e.getMessage().substring("Server returned HTTP response code: ".length()).split(" ")[0];
                rst.status = Integer.valueOf(tmp);
                logger.error(e);
                logger.error(requestXml);
            }else{
                throw e;
            }
        }
        return rst;
    }    
}

你可能感兴趣的:(第三方接口 CallNetApi (自己写connection))