参考文章:https://www.cnblogs.com/zhangxiaoyong/p/8317229.html

顺丰需求文档:

链接:https://pan.baidu.com/s/16EEaphJu2MI9gEvR30pDwQ
提取码:ogry

 

我用的web API实现功能的

电商专递下单功能

 static string xmlstr = "<Request service='OrderService' lang='zh-CN'><Head>erptest</Head><Body><Order orderid='M689082997131401_14' platform_order_id='ML28082997131401' platform_code='JUNXIN' erp_code='0000' express_type='9' j_company='JIELISI' j_contact='JIELISI' j_tel='12344' j_mobile='1234' j_province='GUANGDONG' j_city='FOSHAN' j_address=' GUANGDONG ,CHIAN' d_company='Brazil' d_contact='Leonel' d_tel='12345678' d_mobile='12345678' d_province='Minas Gerais' d_city='Sao Geraldo ' d_address='hubei ' parcel_quantity='1' pay_method='1' declared_value='1.00' declared_value_currency='USD' j_country='CN' j_post_code='528000' d_country='BR' d_post_code='36530000' cargo_total_weight='0.030' operate_flag='1' isBat='1'><Cargo name='Rubber Chin Fixing Band' count='1' unit='PCE' weight='0.030' amount='1.00' cargo_desc='ST-T475-BL' currency='USD' cname='橡胶' hscode='3926909090'/></Order></Body></Request>";

/// <summary>
        /// 下单操作方法webService
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string GetWebService(OrderService model)
        {
            //得到下单XML请求体
            var xml = xmlstr; //Getxml(model);
            //生成密匙
            //var pass = Convert.ToBase64String(MD5(xml+Checkword));
            string verifyCode = digest(xml + Checkword);//这是进一步封装好的
            //下单请求  http post模式
           // var str = GethttpBack(Posturl, "xml=" + xml + "&verifyCode=" + pass);
            //下单请求web service模式
            var str1 = SfKtsServiceAsync(xml, verifyCode).Result;
            return str1;
        }


    /// <summary>
        /// 文档加密verifyCode的生成
        /// </summary>
        /// <param name="toVerifyText"></param>
        /// <returns></returns>
        static string digest(string toVerifyText)
        {
            //  verifyCode的生成规则为:先把xml与checkword前后连接。把连接后的字符串做MD5编码。
            //把MD5编码后的数据进行Base64编码,此时编码后的字符串即为verifyCode。
            // MD5
            byte[] md5 = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(toVerifyText));
            // Base64
            string base64 = Convert.ToBase64String(md5);
            return base64;
        }

 /// <summary>
        /// 调用顺丰接口
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="verifyCode"></param>
        /// <returns></returns>
        public static async Task<string> SfKtsServiceAsync(string xml, string verifyCode)
        {
            var client = new SfKtsServiceClient();
            string result = await client.sfKtsServiceAsync(xml, verifyCode);
            //从XML文件中反序列化为对象   
            SfOrderResponse sfresponse;
            if (Convert.ToString(GetNodeValue(result, "Head")) == "ERR")
            {
                result = XElement.Parse(result).Value;
                ERROrderResponse ErrResonpse = new ERROrderResponse() { code = GetXmlNodeValue(result, "ERROR", "code") };
            }
            else
            {
                sfresponse = new SfOrderResponse()
                {
                    //获取xml中orderid、mailno、destcode等节点值
                    orderid = GetXmlNodeValue(result, "OrderResponse", "orderid"),
                    mailno = GetXmlNodeValue(result, "OrderResponse", "mailno"),
                    agent_mailno = GetXmlNodeValue(result, "OrderResponse", "agent_mailno"),
                    direction_code = GetXmlNodeValue(result, "OrderResponse", "direction_code")
                };
                UsersInfoDal dal = new UsersInfoDal();  //如果不需要插入到数据库中可以注释掉
                await dal.Add(sfresponse);//插入到数据库中
                //测试面单获取
                //string Url = "http://oms.uat.trackmeeasy.com/ruserver/api/getLabelUrl.action?mailno=" + sfresponse.mailno + "&orderid=" + sfresponse.orderid+ "&onepdf=false&jianhuodan=false&username=test&signature=BaZxxmrv6hJMwIt26m0wuw%3D%3D";
                //var s = SfExpress.GetUrltoHtml(Url, "UTF8");
            }
            return result;
        }

需要定义一个返回结果类   SfOrderResponse

/// <summary>
    /// 因为下单成功会返回订单号和运单号 以及筛单结果,所以我们先定义一个返回的响应报文模型容器
    /// </summary>
    public class SfOrderResponse
    {
        /// <summary>
        ///     订单号
        /// </summary>
        public string orderid { get; set; }

        /// <summary>
        ///     运单号
        /// </summary>

        public string mailno { get; set; }

        /// <summary>
        ///     筛单结果 1 人工确认 2 可收派 3 不可以收派
        /// </summary>
       // public string filter_result { get; set; }
        /// <summary>
        /// 流向代码
        /// </summary>
        public string direction_code { get; set; }
        /// <summary>
        /// 代理运单号(仅限10:挂号件才会有) 不是必填项
        /// </summary>
        public string agent_mailno { get; set; }
    }
    /// <summary>
    /// 失败响应
    /// </summary>
    public class ERROrderResponse
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        public string code { get; set; }
    }

面单获取

 #region 面单获取HTTPwebRequest   ML28082997131401_14   21190111_115140" mailno="SF6042002242203
        //string Url = "http://oms.uat.trackmeeasy.com/ruserver/api/getLabelUrl.action";  
        // string Url = "http://oms.uat.trackmeeasy.com/ruserver/api/getLabelUrl.action?mailno=SF6042002242203&orderid=21190111_115140&onepdf=false&jianhuodan=false&username=test&signature=BaZxxmrv6hJMwIt26m0wuw%3D%3D";
        /// <summary>
        /// 根据URL地址获取网页信息
        /// </summary>
        /// <param name="Url">地址</param>
        /// <param name="type">编码格式</param>
        /// <returns></returns>
        public static string GetUrltoHtml(string Url, string type)
        {
            string result = "";
            try
            {
                //对统一资源标识符(URL)发出请求这是abstract
                System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
                // 获取响应实例
                System.Net.WebResponse wResp = wReq.GetResponse();
                //提供字节序列的一般视图
                System.IO.Stream respStream = wResp.GetResponseStream();
                // Dim reader As StreamReader = New StreamReader(respStream)
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))//, Encoding.GetEncoding(type
                {
                    result=reader.ReadToEnd();//返回的是json格式的result
                    JsonTos(result);//把json格式转成实体模型
                    return result;
                }
            }
            catch (System.Exception ex)
            {
                //errorMsg = ex.Message;
            }
            return result;
        }
        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="URL">url地址</param>
        ///<param name="strPostdata">发送的数据</param>
        ///<returns></returns>
        public static string OpenReadWithHttps(string URL, string strPostdata, string strEncoding)
        {
            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "post";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] buffer = encoding.GetBytes(strPostdata);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
            {
                return reader.ReadToEnd();
            }
        }
        /// <summary>
        /// 把JSON格式的字符串转换成实体对象
        /// </summary>
        public static string JsonTos(string jsonText)
        {
            //JsonSerializer js = new JsonSerializer();
            //JsonReader reader = new JsonTextReader(new StringReader(jsonText));
            //while (reader.Read())
            //{
            //    Console.WriteLine(reader.TokenType + "\t\t" + reader.ValueType + "\t\t" + reader.Value);              
            //}
            jsonText = jsonText.Replace("[", "");
            jsonText = jsonText.Replace("]", "");
            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
            string Success = jo["success"].ToString();
            string Mailno = jo["array"]["mailno"].ToString();
            string Orderid = jo["array"]["orderid"].ToString();
            string Url = jo["url"].ToString();//PDF文件
            string ConfigId = jo["array"]["configId"].ToString();
            string Version = jo["version"].ToString();
            //创建一个实体对象
            //expressSheet es = new expressSheet()
            //{
            //    mailno=Mailno,
            //    orderid= Orderid              
            //};
            //保存文件到本地
            GetFileFromNetUrl(Url);
            return Url;
        }
        /// <summary>
        /// 下载文件并保存文件到本地
        /// </summary>
        /// <param name="url"></param>
        private static void GetFileFromNetUrl(string url)
        {
            try
            {
                System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                req.Method = "GET";
                //获得用户名密码的Base64编码  添加Authorization到HTTP头 不需要的账号密码的可以注释下面两行代码
                //string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "userName", "passWord")));
               // req.Headers.Add("Authorization", "Basic " + code);
                byte[] fileBytes;
                using (WebResponse webRes = req.GetResponse())
                {
                    int length = (int)webRes.ContentLength;
                    HttpWebResponse response = webRes as HttpWebResponse;
                    Stream stream = response.GetResponseStream();

                    //读取到内存
                    MemoryStream stmMemory = new MemoryStream();
                    byte[] buffer = new byte[length];
                    int i;
                    //将字节逐个放入到Byte中
                    while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stmMemory.Write(buffer, 0, i);
                    }
                    fileBytes = stmMemory.ToArray();//文件流Byte,需要文件流可直接return,不需要下面的保存代码
                    stmMemory.Close();

                    MemoryStream m = new MemoryStream(fileBytes);
                    string file = string.Format("F:\\666666666666.pdf");//可根据文件类型自定义后缀
                    FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
                    m.WriteTo(fs);
                    m.Close();
                    fs.Close();
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        #endregion

 获取XML节点信息的方法

/// <summary>
        ///     读取XML资源中的指定节点内容
        /// </summary>
        /// <param name="source">XML资源</param>
        /// <param name="nodeName">节点名称</param>
        /// <returns>节点内容</returns>
        public static object GetNodeValue(string source, string nodeName)
        {
            if (source == null || nodeName == null || source == "" || nodeName == "" ||
                source.Length < nodeName.Length * 2) return null;

            var start = source.IndexOf("<" + nodeName + ">") + nodeName.Length + 2;
            var end = source.IndexOf("</" + nodeName + ">");
            if (start == -1 || end == -1)
                return null;
            if (start >= end)
                return null;
            return source.Substring(start, end - start);
        }
        /// <summary>
        ///  获取xml任意节点中某个属性值
        /// </summary>
        /// <param name="strXml">xml</param>
        /// <param name="strNodeName">节点名称</param>
        /// <param name="strValueName">属性名称</param>
        /// <returns></returns>
        public static string GetXmlNodeValue(string strXml, string strNodeName, string strValueName)
        {
            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(strXml);
                var xNode = xmlDoc.SelectSingleNode("//" + strNodeName + "");
                var strValue = xNode.Attributes[strValueName].Value;
                return strValue;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

 

posted on 2019-06-21 10:23  青春似雨后霓虹  阅读(1941)  评论(0编辑  收藏  举报