标签:文档 注意 count res pre error values printf 请求
golang 调用顺丰API接口首先得去https://qiao.sf-express.com/pages/service/API.html这个网站申请注册,拿到我的API接口如下图所示
之后就可以按照文档生成xml请求信息
为了方便快速搭建 golang 的结构粘贴如下
type Rquest struct { XMLName xml.Name `xml:"Request"` Service string `xml:"service,attr"` Lang string `xml:"lang,attr"` Head string `xml:"Head"` Error string `xml:"ERROR"` Body Body `xml:"Body"` } type Body struct { SFOrder SFOrder `xml:"Order"` } type SFOrder struct { XMLName xml.Name `xml:"Order"` OrderId string `xml:"orderid,attr"` ExpressType string `xml:"express_type,attr"` JProvince string `xml:"j_province,attr"` JCity string `xml:"j_city,attr"` JCounty string `xml:"j_county,attr"` JCompany string `xml:"j_company,attr"` JContact string `xml:"j_contact,attr"` JTel string `xml:"j_tel,attr"` JAddress string `xml:"j_address,attr"` DProvince string `xml:"d_province,attr"` DCity string `xml:"d_city,attr"` DCounty string `xml:"d_county,attr"` DCompany string `xml:"d_company,attr"` DContact string `xml:"d_contact,attr"` DTel string `xml:"d_tel,attr"` DAddress string `xml:"d_address,attr"` ParcelQuantity string `xml:"parcel_quantity,attr"` CargoTotalWeight string `xml:"cargo_total_weight,attr"` Custid string `xml:"custid,attr"` PayMethod string `xml:"pay_method,attr"` RoutelabelService string `xml:"routelabelService,attr"` Cargo Cargo } type Cargo struct { XMLName xml.Name `xml:"Cargo"` Name string `xml:"name,attr"` } type Response struct { XMLName xml.Name `xml:"Response"` Service string `xml:"service,attr"` Lang string `xml:"lang,attr"` Head string `xml:"Head"` Error string `xml:"ERROR"` Body RBody `xml:"Body"` } type RBody struct { ResponseBody ResponseBody `xml:"OrderResponse"` } type ResponseBody struct { XMLName xml.Name `xml:"OrderResponse"` FilterResult string `xml:"filter_result,attr"` DestCode string `xml:"destcode,attr"` Mailno string `xml:"mailno,attr"` Origincode string `xml:"origincode,attr"` OrderId string `xml:"orderid,attr"` RlsInfo RlsInfo `xml:"rls_info"` } type RlsInfo struct { RlsErrormsg string `xml:"rls_errormsg,attr"` InvokeResult string `xml:"invoke_result,attr"` RlsCode string `xml:"rls_code,attr"` RlsDetail RlsDetail `xml:"rls_detail"` } type RlsDetail struct { WaybillNo string `xml:"waybillNo,attr"` SourceTransferCode string `xml:"sourceTransferCode,attr"` SourceCityCode string `xml:"sourceCityCode,attr"` SourceDeptCode string `xml:"sourceDeptCode,attr"` SourceTeamCode string `xml:"sourceTeamCode,attr"` DestCityCode string `xml:"destCityCode,attr"` DestDeptCode string `xml:"destDeptCode,attr"` DestDeptCodeMapping string `xml:"destDeptCodeMapping,attr"` DestTeamCode string `xml:"destTeamCode,attr"` DestTransferCode string `xml:"destTransferCode,attr"` DestRouteLabel string `xml:"destRouteLabel,attr"` ProName string `xml:"proName,attr"` CargoTypeCode string `xml:"cargoTypeCode,attr"` LimitTypeCode string `xml:"limitTypeCode,attr"` ExpressTypeCode string `xml:"expressTypeCode,attr"` CodingMapping string `xml:"codingMapping,attr"` XbFlag string `xml:"xbFlag,attr"` PrintFlag string `xml:"printFlag,attr"` TwoDimensionCode string `xml:"twoDimensionCode,attr"` ProCode string `xml:"proCode,attr"` PrintIcon string `xml:"printIcon,attr"` }
将数据结构转换为xml格式化字符串代码如下
xmlorder, _ := xml.Marshal(sfOrder) str2 := string(xmlorder[:])
根据文档提示将信息加密,注意这边是两层加密方式
// str为xml code 为效验码
func md5V(str string, code string) string { md5Key := md5.New() md5Key.Write([]byte(fmt.Sprintf("%s%s", str, code))) xmlKey := base64.StdEncoding.EncodeToString(md5Key.Sum(nil)) return xmlKey }
之后调用http post请求传入两个参数分别为 xml 和 verifyCode
代码如下
func HttpPost(xmls string) (err error, res model.Response) { checkwork := "rIGDqQhU5JbIVQ*******vjcivbg" urls := "http://bsp-oisp.sf-express.com/bsp-oisp/sfexpressService" stringMd5 := md5V(xmls, checkwork) responseData := model.Response{} resp, err := http.PostForm(urls, url.Values{"xml": {xmls}, "verifyCode": {stringMd5}}) if err != nil { return err, responseData } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) err = xml.Unmarshal(body, &responseData) if err != nil { return err, responseData } return err, responseData }
主要的突出问题是:xml的转化,xml与chekwork的加密方式
标签:文档 注意 count res pre error values printf 请求
原文地址:https://www.cnblogs.com/tsxylhs/p/12098476.html