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

  1. //感谢“乌鸦”提供如下的代码
  2. importjava.io.*;
  3. importjava.net.*;
  4. importjavax.xml.parsers.*;
  5. importorg.w3c.dom.*;
  6. publicclassTrackInterface{
  7. privatestaticStringgetSoapRequest(Stringcode,Stringno){
  8. try{
  9. //这里可以改为相对路径,文件为发送请求XML
  10. InputStreamis=newFileInputStream("D:/niceTrackSoap.xml");
  11. InputStreamReaderisr=newInputStreamReader(is);
  12. BufferedReaderreader=newBufferedReader(isr);
  13. Stringsoap="";
  14. Stringtmp;
  15. while((tmp=reader.readLine())!=null){
  16. soap+=tmp;
  17. }
  18. reader.close();
  19. isr.close();
  20. //替换为实际值
  21. soap=soap.replace("${code}$",code);
  22. soap=soap.replace("${no}$",no);
  23. System.out.println(soap);
  24. returnsoap;
  25. }catch(Exceptione){
  26. e.printStackTrace();
  27. returnnull;
  28. }
  29. }
  30. /*
  31. *XML方式请求
  32. */
  33. privatestaticStringgetXMLInputStream(Stringcode,Stringno)
  34. {
  35. try{
  36. URLurl=newURL("http://nicetrack.cn/gettrackresult.aspx?type=xml&code="+code+"&no="+no);
  37. HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
  38. conn.setUseCaches(false);
  39. conn.setDoInput(true);
  40. StringBufferoutStrBf=newStringBuffer("");
  41. //conn.setConnectTimeout(30000);
  42. BufferedReaderin=newBufferedReader(newInputStreamReader(conn.getInputStream(),"UTF-8"));
  43. StringinputLine;
  44. while((inputLine=in.readLine())!=null){
  45. outStrBf.append(inputLine);
  46. }
  47. in.close();
  48. returnoutStrBf.toString();
  49. }catch(Exceptione){
  50. e.printStackTrace();
  51. returnnull;
  52. }
  53. }
  54. /*
  55. *WEBSERVICE方式请求
  56. */
  57. privatestaticInputStreamgetSoapInputStream(Stringcode,Stringno){
  58. try
  59. {
  60. Stringsoap=getSoapRequest(code,no);
  61. if(soap==null){
  62. returnnull;
  63. }
  64. URLurl=newURL("http://nicetrack.cn/express.asmx");
  65. URLConnectionconn=url.openConnection();
  66. conn.setUseCaches(false);
  67. conn.setDoInput(true);
  68. conn.setDoOutput(true);
  69. conn.setConnectTimeout(30000);
  70. conn.setRequestProperty("Host","nicetrack.cn");
  71. conn.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
  72. conn.setRequestProperty("Content-Length",Integer.toString(soap.length()));
  73. OutputStreamos=conn.getOutputStream();
  74. OutputStreamWriterosw=newOutputStreamWriter(os,"utf-8");
  75. osw.write(soap);
  76. osw.flush();
  77. osw.close();
  78. InputStreamis=conn.getInputStream();
  79. returnis;
  80. }catch(Exceptione){
  81. e.printStackTrace();
  82. returnnull;
  83. }
  84. }
  85. //XML方式进行查询
  86. publicstaticStringgetValue(Stringcode,Stringno){
  87. returngetXMLInputStream(code,no);
  88. }
  89. /*
  90. *SOAP方式进行查询
  91. */
  92. publicstaticStringgetValue(Stringcode,Stringno,Stringtype){
  93. try{
  94. Documentdoc;
  95. DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
  96. dbf.setNamespaceAware(true);
  97. DocumentBuilderdb=dbf.newDocumentBuilder();
  98. InputStreamis=getSoapInputStream(code,no);
  99. doc=db.parse(is);
  100. NodeListnl=doc.getElementsByTagName("ChaResult");
  101. Noden=nl.item(0);
  102. StringChaResult=n.getFirstChild().getNodeValue();
  103. is.close();
  104. returnChaResult;
  105. }catch(Exceptione){
  106. e.printStackTrace();
  107. returnnull;
  108. }
  109. }
  110. /**
  111. *@paramargs
  112. *@throwsException
  113. */
  114. publicstaticvoidmain(String[]args){
  115. //TODO
  116. //xml方式调用
  117. System.out.println(TrackInterface.getValue("YUNDA","1200093579386"));
  118. //webservice方式调用
  119. System.out.println(TrackInterface.getValue("YUNDA","1200093579386","SOAP"));
  120. }
  121. }

对应的xml文件内容:

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <soap12:Envelopexmlns: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. <Chaxmlns="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

你可能感兴趣的:(java)