首页 > 代码库 > 快递100接口_java
快递100接口_java
我的案例下载地址:http://download.csdn.net/detail/l294333475/7973501
注意:案例下载完成后只需要替换kuaidi100.java中的key值就可以
servlet层:
package com.liupeng; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.util.kuaidi100; public class Test extends HttpServlet { public Test() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String expressContent = ""; String expressType = "json"; String exsName = "shunfeng"; String exsNum = "211001746537"; //huitongkuaidi 210414794266 //zhongtong 761284432955 //shunfeng 211001746537 if ("shunfeng".equals(exsName) || "ems".equals(exsName)) { //返回HTML页面 expressContent = kuaidi100.searchkuaiDiInfo(exsName, exsNum); expressType = "html"; }else{ //返回的是json expressContent = kuaidi100.getExpressInfo(exsName,exsNum); } request.getSession().setAttribute("expressContent", expressContent); request.getSession().setAttribute("expressType", expressType); request.getRequestDispatcher("test.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } public void init() throws ServletException { } }jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href=http://www.mamicode.com/"">>kuaidi100.java:
package com.util; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class kuaidi100 { private static String key = "XXXXXXXX"; public static void main(String[] agrs) { //huitongkuaidi 210414794266 //zhongtong 761284432955 //shunfeng 211001746537 System.out.println(searchkuaiDiInfo("shunfeng","211001746537")); try { URL url= new URL("http://api.kuaidi100.com/api?id=c1441d4e82940df1&com=shunfeng&nu=211001746537&show=0&muti=1&order=desc"); URLConnection con=url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet=null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return ; if(type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { //String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } System.out.println("content:" + content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 查询快递信息 * @param com 快递公司代码 * @param nu 快递单号 * @return */ public static String getExpressInfo(String com ,String nu){ String ret = ""; try { StringBuilder sb = new StringBuilder(); sb.append("http://api.kuaidi100.com/api?id="); sb.append(kuaidi100.key); sb.append("&com=").append(com); sb.append("&nu=").append(nu); sb.append("&show=0&muti=1&order=desc"); URL url= new URL(sb.toString()); URLConnection con=url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet=null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return ""; if(type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { //String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } ret = content; urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ret; } /** * 该接口使用与收费的快递公司接口查询 * @param com 快递公司代码 * @param nu 快递单号 * @return */ public static String searchkuaiDiInfo(String com, String nu) { String content = ""; try { StringBuilder sb = new StringBuilder(); sb.append("http://www.kuaidi100.com/applyurl?key=").append(kuaidi100.key); sb.append("&com=").append(com); sb.append("&nu=").append(nu); URL url = new URL(sb.toString()); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); byte b[] = new byte[10000]; int numRead = urlStream.read(b); content = new String(b, 0, numRead); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, "UTF-8"); content += newContent; } } urlStream.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("快递查询错误"); } return content; } }快递100接口_java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。