- //感谢“乌鸦”提供如下的代码
- import java.io.*;
- import java.net.*;
- import javax.xml.parsers.*;
- import org.w3c.dom.*;
- public class TrackInterface {
- private static String getSoapRequest(String code,String no) {
- try {
- //这里可以改为相对路径,文件为发送请求XML
- InputStream is=new FileInputStream("D:/niceTrackSoap.xml");
- InputStreamReader isr=new InputStreamReader(is);
- BufferedReader reader=new BufferedReader(isr);
- String soap="";
- String tmp;
- while((tmp=reader.readLine())!=null){
- soap+=tmp;
- }
- reader.close();
- isr.close();
- //替换为实际值
- soap=soap.replace("${code}$", code);
- soap=soap.replace("${no}$", no);
- System.out.println(soap);
- return soap;
- }catch(Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /*
- * XML方式请求
- */
- private static String getXMLInputStream(String code,String no )
- {
- try {
- URL url=new URL("http://nicetrack.cn/gettrackresult.aspx?type=xml&code="+ code + "&no="+ no);
- HttpURLConnection conn=(HttpURLConnection)url.openConnection();
- conn.setUseCaches(false);
- conn.setDoInput(true);
- StringBuffer outStrBf = new StringBuffer("");
- //conn.setConnectTimeout(30000);
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- outStrBf.append(inputLine);
- }
- in.close();
- return outStrBf.toString();
- } catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- /*
- * WEB SERVICE 方式请求
- */
- private static InputStream getSoapInputStream(String code,String no ) {
- try
- {
- String soap=getSoapRequest(code,no);
- if(soap==null) {
- return null;
- }
- URL url=new URL("http://nicetrack.cn/express.asmx");
- URLConnection conn=url.openConnection();
- conn.setUseCaches(false);
- conn.setDoInput(true);
- conn.setDoOutput(true);
- conn.setConnectTimeout(30000);
- conn.setRequestProperty("Host","nicetrack.cn");
- conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
- conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
- OutputStream os=conn.getOutputStream();
- OutputStreamWriter osw=new OutputStreamWriter(os,"utf-8");
- osw.write(soap);
- osw.flush();
- osw.close();
- InputStream is= conn.getInputStream();
- return is;
- } catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- //XML方式进行查询
- public static String getValue(String code,String no) {
- return getXMLInputStream(code,no);
- }
- /*
- * SOAP方式进行查询
- */
- public static String getValue(String code,String no,String type) {
- try {
- Document doc;
- DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db=dbf.newDocumentBuilder();
- InputStream is = getSoapInputStream(code,no);
- doc=db.parse(is);
- NodeList nl= doc.getElementsByTagName("ChaResult");
- Node n = nl.item(0);
- String ChaResult= n.getFirstChild().getNodeValue();
- is.close();
- return ChaResult;
- } catch(Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) {
- // TODO
- //xml 方式调用
- System.out.println(TrackInterface.getValue("YUNDA","1200093579386"));
- //web service 方式调用
- System.out.println(TrackInterface.getValue("YUNDA","1200093579386","SOAP"));
- }
- }
对应的xml文件内容:
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <soap12:Body>
- <Cha xmlns="http://NutsSoft.cn/">
- <code>${code}$</code>
- <no>${no}$</no>
- </Cha>
- </soap12:Body>
- </soap12:Envelope>
感谢NiceTrack.cn的用户“乌鸦”提供如上所示的代码。
本文所示的接口来源于NiceTrack.cn为电子商务网站提供的快递物流查询服务。