/// <summary> /// 转换为FormDataContent /// 为null则返回FormDataContent的实例 /// </summary> /// <exception cref="NotSupportedException"></exception> /// <returns></returns> private MultipartContent CastToFormDataContent() { this.EnsureMediaTypeEqual(FormDataContent.MediaType); if (!(this.Content is MultipartContent httpContent)) { httpContent = new FormDataContent(); } return(httpContent); }
/// <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 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 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; } }
/// <summary> /// 转换为自定义HttpConent的HttpContent /// </summary> /// <returns></returns> public HttpContent ToCustomHttpContext() { var customHttpContent = new FormDataContent(this.boundary); foreach (var httpContent in this) { if (httpContent is ICustomHttpContentConvertable convertable) { customHttpContent.Add(convertable.ToCustomHttpContext()); } else { customHttpContent.Add(httpContent); } } return(customHttpContent); }