虽然关于快递100接口 网上有各种各样的写法,我做的时候研究了一段时间,但是都感觉网上的太复杂,而且说的不太清楚,不太好用,最后我采取了这种写法,简洁易懂!大佬们可以忽略~
首先你要有个id或者说是key
上接口~
public interface ExpressService {
// com 是快递公司编码
//num 是快递单号
String QueryLogistics(String com, String num) throws UnsupportedEncodingException;
}
上实现类~
package com.sixmac.platform.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sixmac.platform.service.ExpressService;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
@Service
public class ExpressServiceImpl implements ExpressService {
public String QueryLogistics(String com, String num) throws UnsupportedEncodingException {
// com 是快递公司编码
//num 是快递单号
String data = "";
BufferedReader in = null;
try {
String postUrl = "http://www.kuaidi100.com/query?id=你自己的id&type=" + com + "&postid=" + num + "&valicode=&temp=0.20180725857720894";
URL realUrl = new URL(postUrl);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
data += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
}
}
return data;
}
}
附上一张快递公司编码图
![快递公司编码](https://i-blog.csdnimg.cn/blog_migrate/cd4711454f65b6b5e64bbc14d9da0905.png)
注意: 目前快递100接口不能查询顺丰快递单号
有关顺丰快递查询的接口后面会更新出来~