/// <summary> /// 添加字段到已有的Content /// 要求content-type为application/x-www-form-urlencoded /// </summary> /// <param name="keyValues">键值对</param> /// <exception cref="NotSupportedException"></exception> /// <returns></returns> public async Task AddFormFieldAsync(IEnumerable <KeyValuePair <string, string> > keyValues) { this.EnsureNotGetOrHead(); this.EnsureMediaTypeEqual(UrlEncodedContent.MediaType); if (keyValues == null) { return; } var formContent = await UrlEncodedContent.FromHttpContentAsync(this.Content).ConfigureAwait(false); await formContent.AddFormFieldAsync(keyValues).ConfigureAwait(false); this.Content = formContent; }
/// <summary> /// 添加字段到已有的Content /// 要求content-type为application/x-www-form-urlencoded /// </summary> /// <param name="keyValues">键值对</param> /// <exception cref="NotSupportedException"></exception> /// <returns></returns> public async Task AddFormFieldAsync(IEnumerable <KeyValuePair <string, string> > keyValues) { this.EnsureNotGetOrHead(); this.EnsureMediaTypeEqual(UrlEncodedContent.MediaType); if (keyValues == null) { return; } var formContent = this.Content as UrlEncodedContent; if (formContent == null) { formContent = new UrlEncodedContent(this.Content); } await formContent.AddFormFieldAsync(keyValues); this.Content = formContent; }
/// <summary> /// 从HttpContent转换得到 /// </summary> /// <param name="httpContent">httpContent实例</param> /// <param name="disposeHttpContent">是否释放httpContent</param> /// <returns></returns> public static async Task <UrlEncodedContent> FromHttpContentAsync(HttpContent httpContent, bool disposeHttpContent = true) { if (httpContent == null) { return(new UrlEncodedContent()); } if (httpContent is UrlEncodedContent urlEncodedContent) { return(urlEncodedContent); } urlEncodedContent = new UrlEncodedContent(); var byteArray = await httpContent.ReadAsByteArrayAsync().ConfigureAwait(false); await urlEncodedContent.AddByteArrayAsync(byteArray).ConfigureAwait(false); if (disposeHttpContent == true) { httpContent.Dispose(); } return(urlEncodedContent); }