/** * 解析根据客户查询域名的xml返回域名信息 * @param 调用客户编号查询域名xml。格式为: * <data> * <method>domain</method> * <dnion> * <tGroup /> * <cGroup /> * <cusnumber>1300000741</cusnumber> * <back>domain</back> * </dnion> *</data> * yanglei * 2015.2.6 * 得到的是一个map,key是域名id value是一个数组,[0]=类型,[1]=名称,[2]=后台客户id */ public Map getDomainList(String cusnumber)throws CDNBandwidthAPIException { Map domainmap = new HashMap(); //获取xml XmlDecryption xmldp = new XmlDecryption(); String strXml =xmldp.XmlToString(xmldp.buildDomainnameXML(null , null , cusnumber));//根据buildDomainnameXML方法得到xml并且调用XmlToString方法转换成字符串 if(strXml != null){ //解析xml获取数据 InputStream in = getBandwidthStream(strXml, 0); SAXBuilder saxbuilder = new SAXBuilder(); if (null != in) { Document doc = null; try { doc = saxbuilder.build(in); } catch (Exception e1) { e1.printStackTrace(); } Element root = doc.getRootElement(); Element msg = root.getChild("msg"); String code = msg.getChildTextTrim("code"); if("100".equals(code)){ Element dnion = root.getChild("dnion"); Element detail = dnion.getChild("detail"); if(detail != null){ List<Element> clist = detail.getChildren(); for(int i = 0;i < clist.size();i++){ String id = clist.get(i).getAttributeValue("dnid"); String type = clist.get(i).getAttributeValue("dtype"); String name = clist.get(i).getAttributeValue("dname"); String custid = clist.get(i).getAttributeValue("custid"); String typename [] = new String[3]; typename[0] = type; typename[1] = name; typename[2] = custid; domainmap.put(id, typename); } } } } } return domainmap; } /** * 调用CDN带宽查询接口返回流 * @param xml 请求接口内容 * @param times 请求次数。默认失败后休眠2秒后重复尝试一次 * @return * @throws CDNBandwidthAPIException */ public InputStream getBandwidthStream(String xml, int times) throws CDNBandwidthAPIException { String bandwidthclient_path = System.getProperty("bandwidthclient_path", "http://dcp.dnion.com/dayuDataReport.action");//调用远程服务器的DayuDataReportAction的execute方法 HttpPost post = new HttpPost(bandwidthclient_path);//用post方式请求 CloseableHttpClient httpclient = HttpClients.createDefault(); StringEntity entity = new StringEntity(ParameraterEncryption.parameraterBlur(xml), "UTF-8");//parameraterBlur方法XML参数加密 post.setEntity(entity); CloseableHttpResponse response = null; try { response = httpclient.execute(post); int result = response.getStatusLine().getStatusCode(); if (result == 200) { InputStream iStream = response.getEntity().getContent(); return iStream; } else{ throw new CDNBandwidthAPIException("Request cdn bandwidth api exception. Remote api return http status code: " + result + " request body: " + xml); } } catch (IOException e) { //接口网络异常,休眠2s后重新尝试一次 if(times < 1){ Threads.sleep(2000); times++; return getBandwidthStream(xml, times); } e.printStackTrace(); return null; } }
设置xml参数,将xml转换成String
package com.dnion.dayu.zk.cdnbusiness; import java.io.IOException; import java.io.StringWriter; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class XmlDecryption{ /** * 生成好带宽获取接口xml * @author yanglei * @date 2014.4.30 * */ public static Document buildBandwidthXML(String method,String begindate,String enddate,String domain,String outlay,String back){ Element root = new Element("data"); Document doc = new Document(root); Element method_element = new Element("method").setText(method); Element elements = new Element("dnion"); elements.addContent(new Element("beginDate").setText(begindate)); elements.addContent(new Element("endDate").setText(enddate)); elements.addContent(new Element("domain").setText(domain)); elements.addContent(new Element("outlay").setText(outlay)); elements.addContent(new Element("back").setText(back)); root.addContent(method_element); root.addContent(elements); return doc; } /** * 将jdom下的Document转换为xml字符串 * @param document jdom下的Document * */ public String XmlToString(Document document){ Format format = Format.getPrettyFormat(); format.setEncoding("UTF-8");//设置编码格式 StringWriter out = new StringWriter(); //输出对象 String sReturn = ""; //输出字符串 XMLOutputter outputter = new XMLOutputter(); try { outputter.output(document, out); } catch (IOException e) { e.printStackTrace(); } sReturn = out.toString(); return sReturn; } }