ApiKD100Service.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package com.kmall.api.service;
  2. import org.springframework.stereotype.Service;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. @Service
  9. public class ApiKD100Service {
  10. //电商加密私钥,快递鸟提供,注意保管,不要泄漏
  11. private String AppKey = "f7a8d954da86787b";
  12. //请求url
  13. private String ReqURL = "http://www.kuaidi100.com/applyurl?key=APPKEY&com=expCode&nu=expNo";
  14. /**
  15. * Json方式 查询订单物流轨迹
  16. *
  17. * @throws Exception
  18. */
  19. public String getOrderTracesByJson(String expCode, String expNo) {
  20. String url = ReqURL.replace("APPKEY", AppKey).replace("expCode", expCode).replace("expNo", expNo);
  21. String html = sendGet(url);
  22. return html;
  23. }
  24. public String sendGet(String urlStr) {
  25. String result = "";
  26. try {
  27. URL url = new URL(urlStr);
  28. URLConnection con = url.openConnection();
  29. con.setAllowUserInteraction(false);
  30. con.setRequestProperty("Connection", "Keep-Alive");
  31. con.setRequestProperty("Charset", "UTF-8");
  32. con.setRequestProperty("Content-Type","text/html; charset=UTF-8");
  33. InputStream urlStream = url.openStream();
  34. String type = con.guessContentTypeFromStream(urlStream);
  35. String charSet = null;
  36. if (type == null)
  37. type = con.getContentType();
  38. if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0)
  39. return null;
  40. if (type.indexOf("charset=") > 0)
  41. charSet = type.substring(type.indexOf("charset=") + 8);
  42. byte b[] = new byte[10000];
  43. int numRead = urlStream.read(b);
  44. result = new String(b, 0, numRead);
  45. while (numRead != -1) {
  46. numRead = urlStream.read(b);
  47. if (numRead != -1) {
  48. String newContent = new String(b, 0, numRead, charSet);
  49. result += newContent;
  50. }
  51. }
  52. //System.out.println("content:" + content);
  53. urlStream.close();
  54. } catch (MalformedURLException e) {
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. return result;
  60. }
  61. }