NiceTrack.cn快递物流跟踪系统接口说明(JAVA版本)

  1. //感谢“乌鸦”提供如下的代码
  2. import java.io.*;
  3. import java.net.*;
  4. import javax.xml.parsers.*;
  5. import org.w3c.dom.*;
  6. public class TrackInterface {
  7.     private static String getSoapRequest(String code,String no) {
  8.         try {
  9.             //这里可以改为相对路径,文件为发送请求XML
  10.             InputStream is=new FileInputStream("D:/niceTrackSoap.xml");
  11.             InputStreamReader isr=new InputStreamReader(is);
  12.             BufferedReader reader=new BufferedReader(isr);
  13.             String soap="";
  14.             String tmp;
  15.             while((tmp=reader.readLine())!=null){
  16.                 soap+=tmp;
  17.             }           
  18.             reader.close();
  19.             isr.close();
  20.        
  21.             //替换为实际值
  22.             soap=soap.replace("${code}$", code);
  23.             soap=soap.replace("${no}$", no);
  24.             
  25.             System.out.println(soap);
  26.             return soap;
  27.         }catch(Exception e) {
  28.             e.printStackTrace();
  29.             return null;
  30.         }
  31.     }
  32.     /*
  33.     * XML方式请求
  34.     */
  35.         private static String getXMLInputStream(String code,String no )
  36.         {
  37.             try   {
  38.                 URL url=new URL("http://nicetrack.cn/gettrackresult.aspx?type=xml&code="+ code + "&no="+ no);
  39.                 HttpURLConnection conn=(HttpURLConnection)url.openConnection(); 
  40.                 conn.setUseCaches(false);
  41.                 conn.setDoInput(true);
  42.                 StringBuffer outStrBf = new StringBuffer("");
  43.                 
  44.                 //conn.setConnectTimeout(30000);
  45.                 
  46.                 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
  47.                 String inputLine;
  48.                 while ((inputLine = in.readLine()) != null) {
  49.                     outStrBf.append(inputLine);
  50.                 }
  51.                 in.close();
  52.                 return outStrBf.toString();
  53.                 
  54.             } catch(Exception e){
  55.                 e.printStackTrace();
  56.                 return null;
  57.             }
  58.         }
  59.     
  60.         
  61.         /*
  62.         * WEB SERVICE 方式请求
  63.         */
  64.         private static InputStream getSoapInputStream(String code,String no ) {
  65.             try
  66.             {
  67.                 String soap=getSoapRequest(code,no);
  68.                 if(soap==null) {
  69.                     return null;
  70.                 }
  71.                 URL url=new URL("http://nicetrack.cn/express.asmx");
  72.                 URLConnection conn=url.openConnection();
  73.                 conn.setUseCaches(false);
  74.                 conn.setDoInput(true);
  75.                 conn.setDoOutput(true);
  76.                 conn.setConnectTimeout(30000);
  77.                 
  78.                 conn.setRequestProperty("Host","nicetrack.cn");
  79.                 conn.setRequestProperty("Content-Type""application/soap+xml; charset=utf-8");
  80.                 conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
  81.                 
  82.                 OutputStream os=conn.getOutputStream();
  83.                 OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
  84.                 osw.write(soap);
  85.                 osw.flush();
  86.                 osw.close();
  87.                 
  88.                 InputStream is= conn.getInputStream();
  89.                 return is; 
  90.             } catch(Exception e){
  91.                 e.printStackTrace();
  92.                 return null;
  93.             }
  94.         }
  95.        
  96.         
  97.         //XML方式进行查询
  98.         public static String getValue(String code,String no) {
  99.                 return getXMLInputStream(code,no);
  100.         }
  101.         
  102.         /*  
  103.          *   SOAP方式进行查询
  104.         */
  105.         public static String getValue(String code,String no,String type) {
  106.             try {
  107.                 Document doc;
  108.                 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
  109.                 dbf.setNamespaceAware(true);
  110.                 DocumentBuilder db=dbf.newDocumentBuilder();
  111.                 InputStream is = getSoapInputStream(code,no);
  112.                 doc=db.parse(is);
  113.                 NodeList nl= doc.getElementsByTagName("ChaResult");
  114.                 Node n = nl.item(0);
  115.                 String ChaResult= n.getFirstChild().getNodeValue();
  116.                 is.close();
  117.                 return ChaResult;
  118.             } catch(Exception e) {
  119.                 e.printStackTrace();
  120.                 return null;
  121.             }
  122.         }
  123.     /**
  124.     * @param args
  125.      * @throws Exception 
  126.     */
  127.     public static void main(String[] args) {
  128.         // TODO
  129.         //xml 方式调用
  130.         System.out.println(TrackInterface.getValue("YUNDA","1200093579386"));
  131.         //web service 方式调用
  132.         System.out.println(TrackInterface.getValue("YUNDA","1200093579386","SOAP"));
  133.     }

对应的xml文件内容:

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  3. <soap12:Body>
  4. <Cha xmlns="http://NutsSoft.cn/">
  5. <code>${code}$</code>
  6. <no>${no}$</no>
  7. </Cha>
  8. </soap12:Body>
  9. </soap12:Envelope>

感谢NiceTrack.cn的用户“乌鸦”提供如上所示的代码。

本文所示的接口来源于NiceTrack.cn为电子商务网站提供的快递物流查询服务。

详情请访问: http://nicetrack.cn/intro/intro.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值