/// <summary> /// 添加文件内容到已有的Content /// 要求content-type为multipart/form-data /// </summary> /// <param name="stream">文件流</param> /// <param name="name">名称</param> /// <param name="fileName">文件名</param> /// <param name="contentType">文件Mime</param> /// <exception cref="NotSupportedException"></exception> public override void AddFormDataFile(Stream stream, string name, string?fileName, string?contentType) { this.EnsureMediaTypeEqual(FormDataContent.MediaType); if (!(this.Content is MultipartContent httpContent)) { httpContent = new FormDataContent(); } var fileContent = new FormDataFileContent(stream, name, fileName, contentType); httpContent.Add(fileContent); this.Content = httpContent; }
/// <summary> /// 添加文本内容到已有的Content /// 要求content-type为multipart/form-data /// </summary> /// <param name="keyValues">键值对</param> /// <exception cref="NotSupportedException"></exception> /// <exception cref="ArgumentNullException"></exception> public override void AddFormDataText(IEnumerable <KeyValue> keyValues) { this.EnsureMediaTypeEqual(FormDataContent.MediaType); if (!(this.Content is MultipartContent httpContent)) { httpContent = new FormDataContent(); } foreach (var keyValue in keyValues) { var textContent = new FormDataTextContent(keyValue); httpContent.Add(textContent); this.Content = httpContent; } }