zoukankan      html  css  js  c++  java
  • 通过WebApi取出XML数据

    Get请求:

     public static Result<GetExpressCollectionResponseType> GetDataFromWebs(string waybillNumbers)
            {
                var result = new Result<GetExpressCollectionResponseType>();
                try
                {
                    var url = "http://***.com.cn/service/Manage/999001/Expresses/GetExpressByEpcodes";
    
                    var client = new WebClient();
                    client.Headers.Add(HttpRequestHeader.Authorization, "basic OTk5MDAxOjEyMzQ1Njc4");
                    client.Headers.Add(HttpRequestHeader.ContentType, "text/xml; charset=utf-8");
    
                    var postbody = "<string>" + waybillNumbers + "</string>";
                    var data = Encoding.UTF8.GetBytes(postbody);
                    var response = client.UploadData(url, "POST", data);
                  
                    GetExpressCollectionResponseType t = default(GetExpressCollectionResponseType);
                    var xmlSerializer = new XmlSerializer(typeof(GetExpressCollectionResponseType));
                    using (Stream xmlStream = new MemoryStream(response))
                    {
                        using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                        {
                            Object obj = xmlSerializer.Deserialize(xmlReader);
                            t = (GetExpressCollectionResponseType)obj;
                        }
                    }
                    result.Entity = t;
                }
                catch (Exception ex)
                {
                    result.HasError = true;
                    result.ErrorMessage = ex.Message;
                }
                return result;
            }

    Post请求:

     public static Result<string> GetLabelFromWeb(string waybillNumber, string filename, string labelSize)
            {
                var result = new Result<string>();
    
                try
                {
                    var url = string.Format("http://***.com.cn/service/Manage/999001/Expresses/{0}/{1}Label",
                        waybillNumber, labelSize);
    
                    var client = new WebClient();
                    client.Headers.Add(HttpRequestHeader.Authorization, "basic OTk5MDAxOjEyMzQ1Njc4");
                    client.Headers.Add(HttpRequestHeader.ContentType, "text/xml; charset=utf-8");
    
                    client.DownloadFile(url, filename);
    
                    result.Entity = filename;
                }
                catch (Exception ex)
                {
                    result.HasError = true;
                    result.ErrorMessage = ex.Message;
                }
                return result;
            }
  • 相关阅读:
    习题训练五 题解
    习题训练四 题解
    习题训练三 题解
    习题训练二 题解
    牛客小白月赛25 解题+补题报告
    习题训练一 题解
    2020.5.10 个人rating赛 解题+补题报告
    2020.4.19 个人rating赛 解题+补题报告
    2020.4.11 组队rating赛 解题+补题报告
    How to Unlock Nissan Micra Smart Key by VVDI Key Tool Plus?
  • 原文地址:https://www.cnblogs.com/xiaoqi742709106/p/4432630.html
Copyright © 2011-2022 走看看