EMS快递查询(包含下载验证码),ems验证码,使用Jsoup下载EMS
分享于 点击 7872 次 点评:166
EMS快递查询(包含下载验证码),ems验证码,使用Jsoup下载EMS
使用Jsoup下载EMS的验证码,使用Jsoup提交查询,返回快递信息。
//EMS查询请求实现类 package in.datashow.kuaidi.services.ems;import in.datashow.kuaidi.CaptchaEntity;import in.datashow.kuaidi.Order;import in.datashow.kuaidi.Order.OrderRecord;import in.datashow.kuaidi.services.QueryService;import java.io.IOException;import java.text.ParseException;import java.util.Date;import java.util.Iterator;import java.util.Map;import org.apache.commons.lang.builder.ToStringBuilder;import org.apache.commons.lang.math.RandomUtils;import org.apache.commons.lang.time.DateUtils;import org.jsoup.Connection.Response;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;public class EmsQueryServiceImpl implements QueryService { public Order query(String orderNum, String captchaCode, Map<String, String> cookies) { Document document = null; try { document = Jsoup .connect("http://www.ems.com.cn/ems/order/singleQuery_t") .referrer( "http://www.ems.com.cn/mailtracking/you_jian_cha_xun.html") .userAgent( "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0") .cookies(cookies).data("mailNum", orderNum).timeout(10000) .data("checkCode", captchaCode).post(); } catch (IOException e1) { e1.printStackTrace(); return null; } String info = document.select(".mailnum_result_box p").text(); Elements rows = document.select(".mailnum_result_box tr"); if (info == null || rows == null) { return null; } Order o = new Order(orderNum); o.setInfo(info); System.out.println(info); if (rows == null || rows.size() == 0) { return null; } rows.remove(0); Iterator<Element> row2 = rows.iterator(); boolean ok = false; while (row2.hasNext()) { Element e = row2.next(); Elements tds = e.select("td"); String dateStr = tds.eq(0).text(); if (dateStr == null || dateStr.replaceAll(" ", "").equals(" ")) { break; } else { ok = true; System.out.println("时间:" + dateStr); } Date date = null; try { date = DateUtils.parseDate( tds.eq(0).text().replaceAll(" ", " ").trim(), new String[] { "yyyy-MM-dd HH:mm:ss" }); } catch (ParseException e1) { e1.printStackTrace(); } String address = tds.eq(1).text(); String status = tds.eq(2).text(); OrderRecord r1 = new OrderRecord(date, address, status); o.addRecords(r1); System.out.println(ToStringBuilder.reflectionToString(r1)); } if (ok) { return o; } else { System.out.println("没有记录"); } return o; } public CaptchaEntity captcha(String orderNum) { Response resp = null; try { resp = Jsoup .connect( "http://www.11183.com.cn/ems/rand?d=" + RandomUtils.nextDouble()) .referrer( "http://www.11183.com.cn/mailtracking/you_jian_cha_xun.html") .userAgent( "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0") .timeout(10000).execute(); } catch (IOException e) { e.printStackTrace(); return null; } Map<String, String> cookies = resp.cookies(); resp.contentType(); byte[] image = resp.bodyAsBytes(); CaptchaEntity captchaEntity = new CaptchaEntity(); captchaEntity.setCookies(cookies); captchaEntity.setImage(image); return captchaEntity; }}//测试类public static void main(String[] args) { QueryService q = new EmsQueryServiceImpl(); String orderNum = "EQ298388415CS"; CaptchaEntity en = q.captcha(orderNum); if (en != null) { try { FileUtils.writeByteArrayToFile(new File( TestEnv.TEMP_FILE_DIRECTORY, System.currentTimeMillis() + ".jpg"), en.getImage()); Scanner s = new Scanner(System.in); String v = s.nextLine(); Order o = q.query(orderNum, v, en.getCookies()); System.out.println(ToStringBuilder.reflectionToString(o)); } catch (IOException e) { e.printStackTrace(); } } }//该片段来自于http://byrx.net
用户点评