常用快递API接口方案.docx
《常用快递API接口方案.docx》由会员分享,可在线阅读,更多相关《常用快递API接口方案.docx(16页珍藏版)》请在冰豆网上搜索。
![常用快递API接口方案.docx](https://file1.bdocx.com/fileroot1/2022-12/1/0eed3dd3-28a2-4201-85d4-04e9201b894c/0eed3dd3-28a2-4201-85d4-04e9201b894c1.gif)
常用快递API接口方案
常用快递API接口方案(总11页)
常用快递API接口方案
电商企业从接收订单-配货-打印快递单到后期的快递员收件-快递配送-轨迹查询-签收短信通知等,都需要快递接口的接入。
整理目前较为常见的接口demo如下:
对接流程:
快递鸟网站申请接口—对接—联调测试—上线
预约取件-在线预约寄件
import
import
publicclassKdGoldAPIDemo{
....
returnresult;
}
/**
*MD5加密
*@paramstr内容
*@paramcharset编码方式
*@throwsException
*/
@SuppressWarnings("unused")
privateStringMD5(Stringstr,Stringcharset)throwsException{
MessageDigestmd=("MD5");
(charset));
byte[]result=();
StringBuffersb=newStringBuffer(32);
for(inti=0;i<;i++){
intval=result[i]&0xff;
if(val<=0xf){
("0");
}
(val));
}
return().toLowerCase();
}
/**
*base64编码
*@paramstr内容
*@paramcharset编码方式
*@throwsUnsupportedEncodingException
*/
privateStringbase64(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringencoded=(charset));
returnencoded;
}
@SuppressWarnings("unused")
privateStringurlEncoder(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringresult=(str,charset);
returnresult;
}
/**
*电商Sign签名生成
*@paramcontent内容
*@paramkeyValueAppkey
*@paramcharset编码方式
*@throwsUnsupportedEncodingException,Exception
*@returnDataSign签名
*/
@SuppressWarnings("unused")
privateStringencrypt(Stringcontent,StringkeyValue,Stringcharset)throwsUnsupportedEncodingException,Exception
{
if(keyValue!
=null)
{
returnbase64(MD5(content+keyValue,charset),charset);
}
returnbase64(MD5(content,charset),charset);
}
/**
*向指定URL发送POST方法的请求
*@paramurl发送请求的URL
*@paramparams请求的参数集合
*@return远程资源的响应结果
*/
@SuppressWarnings("unused")
privateStringsendPost(Stringurl,Mapparams){
OutputStreamWriterout=null;
BufferedReaderin=null;
StringBuilderresult=newStringBuilder();
try{
URLrealUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)();
....
returnresult;
}
/**
*MD5加密
*@paramstr内容
*@paramcharset编码方式
*@throwsException
*/
@SuppressWarnings("unused")
privateStringMD5(Stringstr,Stringcharset)throwsException{
MessageDigestmd=("MD5");
(charset));
byte[]result=();
StringBuffersb=newStringBuffer(32);
for(inti=0;i<;i++){
intval=result[i]&0xff;
if(val<=0xf){
("0");
}
(val));
}
return().toLowerCase();
}
/**
*base64编码
*@paramstr内容
*@paramcharset编码方式
*@throwsUnsupportedEncodingException
*/
privateStringbase64(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringencoded=(charset));
returnencoded;
}
@SuppressWarnings("unused")
privateStringurlEncoder(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringresult=(str,charset);
returnresult;
}
/**
*电商Sign签名生成
*@paramcontent内容
*@paramkeyValueAppkey
*@paramcharset编码方式
*@throwsUnsupportedEncodingException,Exception
*@returnDataSign签名
*/
@SuppressWarnings("unused")
privateStringencrypt(Stringcontent,StringkeyValue,Stringcharset)throwsUnsupportedEncodingException,Exception
{
if(keyValue!
=null)
{
returnbase64(MD5(content+keyValue,charset),charset);
}
returnbase64(MD5(content,charset),charset);
}
/**
*向指定URL发送POST方法的请求
*@paramurl发送请求的URL
*@paramparams请求的参数集合
*@return远程资源的响应结果
*/
@SuppressWarnings("unused")
privateStringsendPost(Stringurl,Mapparams){
OutputStreamWriterout=null;
BufferedReaderin=null;
StringBuilderresult=newStringBuilder();
try{
URLrealUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)();
....
returnresult;
}
/**
*MD5加密
*@paramstr内容
*@paramcharset编码方式
*@throwsException
*/
@SuppressWarnings("unused")
privateStringMD5(Stringstr,Stringcharset)throwsException{
MessageDigestmd=("MD5");
(charset));
byte[]result=();
StringBuffersb=newStringBuffer(32);
for(inti=0;i<;i++){
intval=result[i]&0xff;
if(val<=0xf){
("0");
}
(val));
}
return().toLowerCase();
}
/**
*base64编码
*@paramstr内容
*@paramcharset编码方式
*@throwsUnsupportedEncodingException
*/
privateStringbase64(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringencoded=base64Encode(charset));
returnencoded;
}
@SuppressWarnings("unused")
privateStringurlEncoder(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringresult=(str,charset);
returnresult;
}
/**
*电商Sign签名生成
*@paramcontent内容
*@paramkeyValueAppkey
*@paramcharset编码方式
*@throwsUnsupportedEncodingException,Exception
*@returnDataSign签名
*/
@SuppressWarnings("unused")
privateStringencrypt(Stringcontent,StringkeyValue,Stringcharset)throwsUnsupportedEncodingException,Exception
{
if(keyValue!
=null)
{
returnbase64(MD5(content+keyValue,charset),charset);
}
returnbase64(MD5(content,charset),charset);
}
/**
*向指定URL发送POST方法的请求
*@paramurl发送请求的URL
*@paramparams请求的参数集合
*@return远程资源的响应结果
*/
@SuppressWarnings("unused")
privateStringsendPost(Stringurl,Mapparams){
OutputStreamWriterout=null;
BufferedReaderin=null;
StringBuilderresult=newStringBuilder();
try{
URLrealUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)();
....
returnresult;
}
/**
*MD5加密
*@paramstr内容
*@paramcharset编码方式
*@throwsException
*/
@SuppressWarnings("unused")
privateStringMD5(Stringstr,Stringcharset)throwsException{
MessageDigestmd=("MD5");
(charset));
byte[]result=();
StringBuffersb=newStringBuffer(32);
for(inti=0;i<;i++){
intval=result[i]&0xff;
if(val<=0xf){
("0");
}
(val));
}
return().toLowerCase();
}
/**
*base64编码
*@paramstr内容
*@paramcharset编码方式
*@throwsUnsupportedEncodingException
*/
privateStringbase64(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringencoded=base64Encode(charset));
returnencoded;
}
@SuppressWarnings("unused")
privateStringurlEncoder(Stringstr,Stringcharset)throwsUnsupportedEncodingException{
Stringresult=(str,charset);
returnresult;
}
/**
*电商Sign签名生成
*@paramcontent内容
*@paramkeyValueAppkey
*@paramcharset编码方式
*@throwsUnsupportedEncodingException,Exception
*@returnDataSign签名
*/
@SuppressWarnings("unused")
privateStringencrypt(Stringcontent,StringkeyValue,Stringcharset)throwsUnsupportedEncodingException,Exception
{
if(keyValue!
=null)
{
returnbase64(MD5(content+keyValue,charset),charset);
}
returnbase64(MD5(content,charset),charset);
}
/**
*向指定URL发送POST方法的请求
*@paramurl发送请求的URL
*@paramparams请求的参数集合
*@return远程资源的响应结果
*/
@SuppressWarnings("unused")
privateStringsendPost(Stringurl,Mapparams){
OutputStreamWriterout=null;
BufferedReaderin=null;
StringBuilderresult=newStringBuilder();
try{
URLrealUrl=newURL(url);
HttpURLConnectionconn=(HttpURLConnection)();
//发送POST请求必须设置如下两行
(true);
(true);
//POST方法
("POST");
//设置通用的请求属性
("accept","*/*");
("connection","Keep-Alive");
("user-agent",
"Mozilla/(compatible;MSIE;WindowsNT;SV1)");
("Content-Type","application/x-www-form-urlencoded");
();
//获取URLConnection对象对应的输出流
out=newOutputStreamWriter(),"UTF-8");
//发送请求参数
if(params!
=null){
StringBuilderparam=newStringBuilder();
forentry:
()){
if()>0){
("&");
}
());
("=");
());
":
"+());
}
"param:
"+());
());
}
//flush输出流的缓冲
();
//定义BufferedReader输入流来读取URL的响应
in=newBufferedReader(
newInputStreamReader(),"UTF-8"));
Stringline;
while((line=())!
=null){
(line);
}
}catch(Exceptione){
();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!
=null){
();
}
if(in!
=null){
();
}
}
catch(IOExceptionex){
();
}
}
return();
}
privatestaticchar[]base64EncodeChars=newchar[]{
'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
publicstaticStringbase64Encode(byte[]data){
StringBuffersb=newStringBuffer();
intlen=;
inti=0;
intb1,b2,b3;
while(ib1=data[i++]&0xff;
if(i==len)
{
(base64EncodeChars[b1>>>2]);
(base64EncodeChars[(b1&0x3)<<4]);
("==");
break;
}
b2=data[i++]&0xff;
if(i==len)
{
(base64EncodeChars[b1>>>2]);
(base64EncodeChars[((b1&0x03)<<4)|((b2&0xf0)>>>4)]);
(base64EncodeChars[(b2&0x0f)<<2]);
("=");
break;
}
b3=data[i++]&0xff;
(base64EncodeChars[b1>>>2]);
(base64EncodeChars[((b1&0x03)<<4)|((b2&0xf0)>>>4)]);
(base64EncodeChars[((b2&0x0f)<<2)|((b3&0xc0)>>>6)]);
(base64EncodeChars[b3&0x3f]);
}
return();
}
}