民航接口

来源:互联网 发布:mac mysql 密码修改 编辑:程序博客网 时间:2024/05/01 13:26

民航接口调用过程
1、读取配置文件UtilTools 类读取xml为String

public static String readFile(String file) {  file=UtilTools.class.getClassLoader().getResource(file).getFile();  String xml="";  BufferedReader br = null;    try {     File inFile = new File(file);     InputStream in = new FileInputStream(inFile);     br = new BufferedReader(new InputStreamReader(in));     String buffer = new String();     String line = "";     while ((line = br.readLine()) != null) {      buffer+=line;     }     xml = buffer.toString();    } catch (Exception e) {     System.out.println("读取失败");    } finally {     if (br != null) {      try {       br.close();      } catch (IOException e1) {       // TODO 自动生成 catch 块       e1.printStackTrace();      }     }    }    return xml; } public static byte[] readInputStream(InputStream inStream) throws Exception {   ByteArrayOutputStream outStream = new ByteArrayOutputStream();   byte[] buffer = new byte[1024];   int len = 0;   while ((len = inStream.read(buffer)) != -1) {    outStream.write(buffer, 0, len);   }   byte[] data = outStream.toByteArray(); // 网页的二进制数据   outStream.close();   inStream.close();   return data;  }

 

2. CAEBase类

 

public class CAEBase { private static String xml = UtilTools.readFile(Constants.DEFINEFILE_CAE); public static void main(String[] args) {  try {   List l = CAEBase.queryCAEInfo("CAE376446119");   System.out.println(((List) l.get(0)).get(0));   for (int i = 0; i < l.size(); i++) {    ((List) (l.get(i))).get(0);   }  } catch (Exception e) {   // TODO 自动生成 catch 块   e.printStackTrace();  } } public static List queryCAEInfo(String caeCode) throws Exception {  if ("".matches(caeCode)) {   return null;  }  List list = null;  //System.out.println(xml.replaceAll("CAECode", caeCode));  //设置代理  Properties prop = System.getProperties();  prop.put("proxySet", "false");  prop.remove("http.proxyUser");  prop.remove("http.proxyPassword");  prop.setProperty("http.proxyHost", Constants.PROXYHOST);  prop.setProperty("http.proxyPort", Constants.PORT);  byte[] buf = xml.replaceAll("CAECode", caeCode).getBytes("utf-8");  String urlString = "http://211.103.158.178:8008/Service1.asmx";  String soapActionString = "http://CAEServicesForGE.com/xmlMailTrace";  URL url = new URL(urlString);  HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  httpConn.setRequestProperty(   "Content-Length",   String.valueOf(buf.length));  httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");  httpConn.setRequestProperty("soapActionString", soapActionString);  httpConn.setRequestMethod("POST");  httpConn.setDoOutput(true);  httpConn.setDoInput(true);  //  proxy user and pass  httpConn.setRequestProperty(   "Proxy-Authorization",   "Basic "    + new sun.misc.BASE64Encoder().encode(     (Constants.USERNAME + ":" + Constants.PASSWORD)      .getBytes()));  OutputStream out = httpConn.getOutputStream();  out.write(buf);  out.close();  httpConn.connect();  byte[] datas = UtilTools.readInputStream(httpConn.getInputStream());  prop.setProperty("http.proxyHost", "");  prop.setProperty("http.proxyPort", "");  prop.setProperty("http.proxyUser", "");  prop.setProperty("http.proxyPassword", "");  String result =   new String(datas, "utf-8").replaceAll("<", "<").replaceAll(    ">",    ">");

StringReader read = new StringReader(result);  InputSource source = new InputSource(read);  SAXBuilder sb = new SAXBuilder();  ByteArrayInputStream bia =   new ByteArrayInputStream(result.getBytes("utf-8"));

  org.jdom.Document myDocument = sb.build(bia);

  Element rootElement = myDocument.getRootElement(); // 取的根元素

  //System.out.println(rootElement.getName()); // 输出根元素的名称(测试)

  // 获取Body节点  List jd =   (    (Element)     (      (Element) ((Element) ((Element) rootElement       .getChildren()       .get(0))     .getChildren()     .get(0))    .getChildren()    .get(0))    .getChildren()    .get(0))    .getChildren();  // 输出根元素的名称(测试)  if(jd.size()!=0){   list=new ArrayList();  }  for (int i = 0; i < jd.size(); i++) {   Element e = (Element) jd.get(i);   ArrayList temp = new ArrayList();   temp.add(((Element) e.getChildren().get(0)).getText()); //操作时间   temp.add(((Element) e.getChildren().get(1)).getText()); //营业点   temp.add(((Element) e.getChildren().get(2)).getText()); //所在机构   temp.add(((Element) e.getChildren().get(3)).getText()); //事件   temp.add(((Element) e.getChildren().get(4)).getText()); //详细说明   list.add(temp);  }    return list; }

}

若数据缺失遍历方式则:

ByteArrayInputStream bia =new ByteArrayInputStream(result.getBytes("utf-8"));org.jdom.Document myDocument = sb.build(bia);Element rootElement = myDocument.getRootElement(); // 取的根元素//System.out.println(rootElement.getName()); // 输出根元素的名称(测试)// 获取Body节点List jd =((Element)((Element) ((Element) ((Element) rootElement.getChildren().get(0)).getChildren().get(0)).getChildren().get(0)).getChildren().get(0)).getChildren();// 输出根元素的名称(测试)if(jd.size()!=0){list=new ArrayList();}for (int i = 0; i < jd.size(); i++) {ArrayList temp = new ArrayList(); //保存每行需要的字符串 顺序为:操作时间,营业点,所在机构,事件,详细说明temp.add("");temp.add("");temp.add("");temp.add("");temp.add("");//只有“操作时间”和“所在机构”是必填数据项 
List e = ((Element)jd.get(i)).getChildren();//取出每行数据转为Element进行获取数据 防止数据缺失异常for(int j = 0 ; j < e.size() ; j++) {Element tempNode =(Element) e.get(j);if("操作时间".equals(tempNode.getName().trim())){temp.set(0, tempNode.getTextTrim());}else if("营业点".equals(tempNode.getName().trim())){temp.set(1, tempNode.getTextTrim());}else if("所在机构".equals(tempNode.getName().trim())){temp.set(2, tempNode.getTextTrim());}else if("事件".equals(tempNode.getName().trim())){temp.set(3, tempNode.getTextTrim());}else if("详细说明".equals(tempNode.getName().trim())){temp.set(4, tempNode.getTextTrim());}}list.add(temp);}return list;}


 

3、cae.xml 可以直接从http://211.103.158.178:8008/Service1.asmx?op=xmlMailTrace读取

      <_strKDDH>CAECode</_strKDDH>
      <_strUsername>C*****</_strUsername>
      <_strPassword>C****</_strPassword>



 

0 0
原创粉丝点击