快递100 物流接口对接

  • 到快递100(https://www.kuaidi100.com/openapi/applyapi.shtml),申请免费版本。
  • 快递100授权key

  • 快递查询API(适用于除EMS、顺丰、申通、圆通、中通、韵达之外的公司,可自定义公司与单号,返回xml与json数据,有几家快递公司不支持):http://www.kuaidi100.com/openapi/api_post.shtml
  • HtmlAPI(适用于所有包括EMS、顺丰、申通、圆通、中通、韵达在内的公司,可自定公司与单号,请求后返回一个url,需要通过iframe来嵌套该url来显示最终结果,结果下方有快递100手机端的广告banner):http://www.kuaidi100.com/download/html_api(20140729).doc
  • JAVA 版本:
  • /**
     * 解析结果封装成map
     * @param com 快递公司简称
     * @param nu 快递单号
     */
    public static Map<String,Object> getResultLogistics(String com,String nu){
       Map<String,Object> result = new HashMap<String,Object>();
       String key="2ecf1eadd1cddaa9";//授权KEY 换成自己的即可
       try {
          String str = getLogisticsInformation(key,com,nu);
          if(StringUtil.isEmpty(str)){
             result.put("status", "3");//url配置错误
             result.put("message", "接口异常");
             return result;
          }
          result.put("kuaidiUrl",str);
          result.put("status","1");
       } catch (Exception e) {
          log.error(e);
          e.printStackTrace();
       }
       return result;
    }
    /**
     * 查询快递结果
     * @param key 授权密匙(Key)
     * @param com 快递公司简称
     * @param nu 快递单号
     * @return String
        * @throws Exception
        */
    public static String getLogisticsInformation(String key,String com,String nu) throws Exception{
       String url="http://www.kuaidi100.com/applyurl?key\=%s&com\=%s&nu\=%s";
       url = String.format(url, key,com,nu);
       String result = null;
       HttpClient httpClient = new HttpClient();
       GetMethod getMethod = new GetMethod(url);
       httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");
       getMethod.getParams().setParameter("http.method.retry-handler", new DefaultHttpMethodRetryHandler());
       int statusCode = httpClient.executeMethod(getMethod);
       if (statusCode == 200){
          StringBuffer temp = new StringBuffer();
          InputStream in = getMethod.getResponseBodyAsStream();
          BufferedReader buffer = new BufferedReader(
                new InputStreamReader(in, "UTF-8"));
          for (String tempstr = ""; (tempstr = buffer.readLine()) != null;)
             temp = temp.append(tempstr);
          buffer.close();
          in.close();
          result = temp.toString().trim();
       }
       return result;
    }
    JSP页面展示:
  • <c:if test="${!empty statusMap }">
       <c:forEach items="${statusMap}" var="status">
          <c:if test="${'1'==status.value}">
             <iframe name="kuaidi100" id="kuaidi100" style="margin-left: -100px;" src="${kuaidiUrl}" width="550" height="380" marginwidth="0" marginheight="0"
                   hspace="0" vspace="0" frameborder="0" scrolling="no">
    
             </iframe>
          </c:if>
          <c:if test="${'1'!=status.value}">
    
          </c:if>
       </c:forEach>
    </c:if>

猜你喜欢

转载自blog.csdn.net/u012833739/article/details/70186100