private MultipartFormDataContent Build(Dictionary <string, object> dataPair) { string timeStamp = this.GetTimeStamp(); MultipartFormDataContent multipart = new MultipartFormDataContent(); try { string json = dataPair[OTHER] as string; Dictionary <string, string> jsonMap = JsonConvert.DeserializeObject <Dictionary <string, string> >(json); foreach (string key in jsonMap.Keys) { string value = jsonMap[key] as string; if (value != null) { multipart.Add(new StringContent(value), key); continue; } } dataPair.Remove(OTHER); } catch { } 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; } 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); }
private MultipartFormDataContent Build(Dictionary <string, object> dataPair) { MultipartFormDataContent multipart = new MultipartFormDataContent(); string timeStamp = 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; } FileInfo file = dataPair[key] as FileInfo; if (file != null) { var fileContent = new ByteArrayContent(File.ReadAllBytes(file.FullName)); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = file.Name, Name = key, }; multipart.Add(fileContent); } } return(multipart); }
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; }