Ejemplo n.º 1
0
        /// <summary>
        /// HW API Post方法Restsharp版
        /// </summary>
        /// <typeparam name="T">传入参数的类型</typeparam>
        /// <param name="baseUrl">基础URL</param>
        /// <param name="uri">资源链接</param>
        /// <param name="accessToken">令牌</param>
        /// <param name="objToPost">要传输的实体,类型由T给定</param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static string HuaweiPostSyncRest <T>(string baseUrl, string uri, string accessToken, T objToPost, params Cookie[] cookies)
        {
            //发送HTTPS请求
            if (baseUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                HwApiHelper.SetCertificatePolicy();
            }
            var client = new RestClient(baseUrl);

            var request = new RestRequest(uri);

            request.Timeout = timeout;

            request.Method = Method.POST;
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Authorization", "Bearer " + accessToken.Trim());
            request.AddHeader("soapAction", "");
            request.AddJsonBody(objToPost);//直接传输一个实体,无需如HttpWebRequest那样,先序列化,编码成byte[],再通过WriteSteam写
            if (cookies != null)
            {
                foreach (var c in cookies)
                {
                    request.AddCookie(c.Name, c.Value);
                }
            }
            //方式1:按Json格式返回响应文本
            IRestResponse response = client.Execute(request);

            return(response.Content);//按指定的格式返回响应文本,如json
        }
Ejemplo n.º 2
0
        internal Stream DownloadToStream(FileDownloadInput input)
        {
            string json   = JsonConvert.SerializeObject(input);
            var    res    = HwApiHelper.HuaweiPostSync(baseUrl, downloadUri, token.Access_token, json, null);
            Stream stream = res.GetResponseStream();

            return(stream);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 产生PO的PDF文件。
        /// 产生的文件保存在出参的流中
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns></returns>
        internal GenPoPdfOutParameter GenPoPdfDAL(GenPoPdfInput input)
        {
            string json    = JsonConvert.SerializeObject(input);
            var    res     = HwApiHelper.HuaweiPostSync(baseUrl, genPoPdfUri, token.Access_token, json, null);
            string resJson = HwApiHelper.GetResponseString(res);
            var    output  = JsonConvert.DeserializeObject <GenPoPdfOutParameter>(resJson);

            ErrorMsg = output.HttpCode;
            ErrorMsg = output.Message;
            return(output);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// RestSharp版PO列表查询
        /// </summary>
        /// <param name="inParameter"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        internal PoLineListOutput GetPoLineListRest(PoLineListInput inParameter, int page, int pageSize)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };
            string json    = JsonConvert.SerializeObject(inParameter, settings);
            string url     = pageSize == 100 ? findPoLineListUri + page : findPoLineListUri + pageSize + "/" + page;
            var    resJson = HwApiHelper.HuaweiPostSyncRest(baseUrl, url, token.Access_token, json, null);

            PoLineListOutput output = JsonConvert.DeserializeObject <PoLineListOutput>(resJson);

            return(output);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 签返订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        internal SignBackPoListOutput SignBackPoList(SignBackPoListInput input)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };
            string json    = JsonConvert.SerializeObject(input, settings);
            var    res     = HwApiHelper.HuaweiPostSync(baseUrl, signBackUri, token.Access_token, json, null);
            string resJson = HwApiHelper.GetResponseString(res);
            var    output  = JsonConvert.DeserializeObject <SignBackPoListOutput>(resJson);

            ErrorCode = output.Code;
            ErrorMsg  = output.Result;
            return(output);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// PO列表查询
        /// </summary>
        /// <param name="inParameter"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        internal PoLineListOutput GetPoLineList(PoLineListInput inParameter, int page, int pageSize)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            };
            string json    = JsonConvert.SerializeObject(inParameter, settings);
            string url     = pageSize == 100 ? findPoLineListUri + page: findPoLineListUri + pageSize + "/" + page;
            var    res     = HwApiHelper.HuaweiPostSync(baseUrl, url, token.Access_token, json, null);
            string resJson = HwApiHelper.GetResponseString(res);
            var    output  = JsonConvert.DeserializeObject <PoLineListOutput>(resJson);

            ErrorMsg  = res.StatusDescription;
            ErrorCode = res.StatusCode.ToString();
            return(output);
        }
Ejemplo n.º 7
0
        public static Token GetToken()
        {
            string uri = "oauth2/token";

            if (File.Exists(SettingItems.settingFile))
            {
                //读配置文件
                MyConfiguration cfg = new MyConfiguration(false);
                cfg.Load(SettingItems.settingFile);
                string baseUrl   = cfg.ReadString("BaseUrl");
                string appKey    = cfg.ReadString("AppKey");
                string appSecret = cfg.ReadString("AppSecret");
                System.Net.HttpWebResponse response = HwApiHelper.CreateHwTokenHttpResponse(baseUrl, uri, appKey, appSecret, null);
                string result = HwApiHelper.GetResponseString(response);
                Token  token  = JsonConvert.DeserializeObject <Token>(result);
                return(token);
            }
            else
            {
                return(null);
            }
        }