Example #1
0
        private MultipartFormDataContent Build(Dictionary<string, object> dataPair)
        {
            MultipartFormDataContent multipart = new MultipartFormDataContent();
            string timeStamp = this.GetTimeStamp();
            dataPair.Add(APPID, _appConfig.AppId);
            dataPair.Add(TIMESTAMP, timeStamp);
            dataPair.Add(SIGN_TYPE, _appConfig.SignType.ToString());

            SignatureHelper sigHelper = new SignatureHelper(_appConfig);
            string formatData = RequestHelper.FormatRequest(dataPair);
            multipart.Add(new StringContent(sigHelper.GetSignature(formatData)), SIGNATURE);

            foreach (string key in dataPair.Keys)
            {
                string value = dataPair[key] as string;
                if (value != null)
                {
                    multipart.Add(new StringContent(value), key);
                    continue;
                }

                bool? boolValue = dataPair[key] as bool?;
                if (boolValue != null)
                {
                    multipart.Add(new StringContent(boolValue.Value ? "true" : "false"), key);
                    continue;
                }

                FileInfo file = dataPair[key] as FileInfo;
                if (file != null)
                {
                    var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(file.FullName));
                    fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
                    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        FileName = file.Name,
                        Name = key,
                    };

                    multipart.Add(fileContent);
                }
            }

            return multipart;
        }