/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.dom4j.Document
 *  org.dom4j.DocumentHelper
 *  org.dom4j.Element
 *  org.dom4j.Node
 */
package com.hst.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

public class DxptCallUtil {
    private static ResourceBundle rb = ResourceBundle.getBundle("config/hst");
    private static String aUrl = rb.getString("hst.DXPT.URL");

    public static void main(String[] args) {
        try {
            String nr = "\u5916\u90e8\u8bc4\u6d4b\u95ee\u5377\u8c03\u67e5\u53d1\u8d77";
            String md = "331200000000000145;331200000000000146;331200000000000147";
            String res = DxptCallUtil.callNsrdxfsServlet("NSRDXFS", "01", nr, md, null, "99999999999");
            Document doc = DocumentHelper.parseText((String)res);
            Node node = doc.selectSingleNode("/DXPT/BODY/FSJG");
            if (node != null) {
                String fsjg = node.getText();
                System.out.println(fsjg);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String callNsrdxfsServlet(String businessId, String yyxtId, String nr, String md, String dsfssj, String fsrDm) throws Exception {
        if (dsfssj == null) {
            dsfssj = "";
        }
        Document doc = DocumentHelper.createDocument();
        doc.setXMLEncoding("GBK");
        Element root = doc.addElement("DXPT");
        Element head = root.addElement("HEAD");
        Element businessid = head.addElement("BUSINESS_ID");
        businessid.addText(businessId);
        Element yyxtid = head.addElement("YYXTID");
        yyxtid.addText(yyxtId);
        Element body = root.addElement("BODY");
        Element dxnr = body.addElement("DXNR");
        dxnr.addCDATA(nr);
        Element nsrmd = body.addElement("NSRMD");
        nsrmd.addText(md);
        Element sj = body.addElement("DSFSSJ");
        sj.addText(dsfssj);
        Element fsr = body.addElement("FSRDM");
        fsr.addText(fsrDm);
        return DxptCallUtil.doCall(doc.asXML());
    }

    private static URLConnection getRemoteAccessConn(String servletURL, String params) {
        HttpURLConnection con = null;
        try {
            URL url = new URL(servletURL);
            con = (HttpURLConnection)url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(true);
            con.setRequestProperty("accept", "*/*");
            con.setRequestProperty("connection", "Keep-Alive");
            con.setRequestProperty("Content-Length", String.valueOf(params.length()));
            return con;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String doCall(String xmlStr) throws Exception {
        StringBuffer sb = new StringBuffer();
        HttpURLConnection urlConn = (HttpURLConnection)DxptCallUtil.getRemoteAccessConn(aUrl, xmlStr);
        PrintWriter pw = null;
        try {
            try {
                urlConn.connect();
                pw = new PrintWriter(new OutputStreamWriter(urlConn.getOutputStream(), "GBK"));
                pw.write(xmlStr);
                pw.flush();
                int responseCode = urlConn.getResponseCode();
                if (responseCode != 200) {
                    System.out.println(" Error===" + responseCode);
                } else {
                    System.out.println("Post Success!");
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                throw e;
            }
        }
        finally {
            pw.close();
        }
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            String s = null;
            while ((s = in.readLine()) != null) {
                sb.append(s).append("\n");
            }
            System.out.println("return str:" + sb.toString());
            in.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        urlConn.disconnect();
        return sb.toString();
    }
}
