(顺丰已换成丰桥接口,老的顺丰开放平台的接口已失效)
由于官方文档非常不完整,接入顺丰API遇到很多问题,遂在此记录问题与总结
1.申请访问令牌access_token
每次使用接口都需要有访问令牌,APPID和APPKEY。这里我们需要用json来封装数据,我使用的是ForceJson,地址:https://github.com/ccwant/ForceJson,代码:
public string sendToken(string transMessageId)
{
String url = "https://open-sbox.sf-express.com/public/v1.0/security/access_token/sf_appid/00053228/sf_appkey/8FFA9ACB5229B5DB1DD9C0BA207DE18A";
JSONObject json = new JSONObject();
JSONObject jsonT = new JSONObject();
json.Put("transType","301");
json.Put("transMessageId",transMessageId);
jsonT.Put("head",json);
string accessToken=HttpPost(url,jsonT.ToString());
return accessToken;
}
*transMessageId:交易流水号,格式如:YYYYMMDD+流水号{10},例如:201404120000000001,此流水号唯一不能重复
发送请求数据,顺丰只能使用post,代码:
//post数据
public string HttpPost(string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/json"; //注意设置为json类型
request.ContentLength = postDataStr.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(postDataStr);
writer.Flush();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
//解析响应报文,先解析出body的数据,在读取AccessToken字段的值
JSONObject json = new JSONObject(retString);
string result = json.GetString("body");
JSONObject json2 = new JSONObject(result);
return json2.GetString("accessToken");
}
2.顺丰快速下单接口
调用AccessToken方法传入请求url,下单的数据格式参照官方文档,直接上代码:
public string CreateOrder(string transMessageId,string orderId,string expressType,int payMethod,int isDocall,int isGenBillno,int isGenEletricPic,string custId,string payArea,string sendStartTime,string needReturnTrackKingNo,string remark,string company,string contact,string tel,string province,string city,string county,string address,string company2,string contact2,string tel2,string province2,string city2,string county2,string address2,string cargo)
{
string accessToken=getToken(transMessageId);
string url = "https://open-sbox.sf-express.com/rest/v1.0/order/access_token/" + accessToken + "/sf_appid/00053228/sf_appkey/8FFA9ACB5229B5DB1DD9C0BA207DE18A";
//head
JSONObject json1 = new JSONObject();
json1.Put("transMessageId", transMessageId);
json1.Put("transType","200");
//deliverInfo 寄件方信息
JSONObject json3 = new JSONObject();
json3.Put("company", company);
json3.Put("contact", contact);
json3.Put("tel", tel);
json3.Put("province", province);
json3.Put("city", city);
json3.Put("county", county);
json3.Put("address", address);
//consigneeInfo 收件方信息
JSONObject json4 = new JSONObject();
json4.Put("company", company2);
json4.Put("contact", contact2);
json4.Put("tel", tel2);
json4.Put("province", province2);
json4.Put("city", city2);
json4.Put("county", county2);
json4.Put("address", address2);
//cargoInfo 货物信息
JSONObject json5 = new JSONObject();
json5.Put("cargo",cargo);
json5.Put("cargoCount","10");
json5.Put("cargoTotalWeight", 1);
json5.Put("cargoUnit","件");
json5.Put("cargoWeight","1");
//addedService 增值服务
JSONObject json6=new JSONObject();
json6.Put("name","CUSTID");
json6.Put("value","5731804046");
JSONArray array=new JSONArray();
array.Put(json6);
//按照官方的请求报文格式整合数据
string bodys = "{'orderId':" + orderId + ",'expressType':'" + expressType + "','isDoCall':" + isDocall + ",'isGenBillno':" + isGenBillno + ",'isGenEletricPic':" + isGenEletricPic + ",'needReturnTrackKingNo':'" + needReturnTrackKingNo + "','payMethod':" + payMethod + ",'remark':" + remark + ",'payArea':" + payArea + ",'sendStartTime':'" + sendStartTime + "','custId':'" + custId + "',cargoInfo:" + json5 + ",consigneeInfo:" + json4 + ",deliverInfo:" + json3 + ",addedServices:"+array+"}";
JSONObject addbodys = new JSONObject(bodys);
string content = "{head:"+json1+",body:"+addbodys+"}";
JSONObject jsonRequest = new JSONObject(content);
return OrderPost(url, jsonRequest.ToString());
}
*每次测试一定要换订单ID,不可重复,否则报错,该订单号自行定义,规范即可
3.打印电子订单
得到的数据为BASE64字符的图片,注意去掉数据里额外的符号,否则不是正确的base64编码,丧心病狂的是数据里还带换行符,不仔细根本发现不了。可在后台解码生成图片,也可以前端直接调用,格式为:<img src="data:image/png;base64编码">,完整代码:
public string getPicture(string transMessageId,string orderId)
{
string accessToken=getToken(transMessageId);
string url = "https://open-sbox.sf-express.com/rest/v1.0/waybill/image/access_token/"+accessToken+"/sf_appid/00053228/sf_appkey/8FFA9ACB5229B5DB1DD9C0BA207DE18A";
JSONObject head = new JSONObject();
head.Put("transType","205");
head.Put("transMessageId",transMessageId);
JSONObject body = new JSONObject();
body.Put("orderId",orderId);
string content = "{head:"+head+",body:"+body+"}";
JSONObject jsonRequest = new JSONObject(content);
//将得到的base64编码转为图片
var base64 = PicPost(url, jsonRequest.ToString());
//去掉数据里的额外部分,注意字符里有\n换行符
base64 = base64.Replace("[", "").Replace("]", "").Replace("\"", "").Replace("\\n", "").Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");
return base64;
}
得到的顺丰电子运单。