/// <summary> /// Read content as a disctionary of {key}={value}. /// </summary> /// <param name="this"> /// The http content. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="@this"/> is null. /// </exception> /// <exception cref="NotSupportedException"> /// If <paramref name="@this"/> is not "text/plain" content. /// </exception> public static async Task <Dictionary <string, string> > ReadAsDictionaryAsync(this HttpContent @this) { if (@this == null) { throw new ArgumentNullException("this"); } if ([email protected]()) { throw new NotSupportedException("ReadAsDictionaryAsync supports only text/plain content."); } var responseValues = new Dictionary <string, string>(); using (var streamReader = new StringReader(await @this.ReadAsStringAsync())) { string responseLine = null; while ((responseLine = await streamReader.ReadLineAsync()) != null) { var firstEqual = responseLine.IndexOf("=", StringComparison.OrdinalIgnoreCase); if (firstEqual > 0) { string name = responseLine.Substring(0, firstEqual); string value = responseLine.Substring(firstEqual + 1, responseLine.Length - (firstEqual + 1)); responseValues.Add(name, WebUtility.UrlDecode(value)); } else { responseValues.Add(responseLine, string.Empty); } } } return(responseValues); }
/// <summary> /// Read content as a Json object <typeparamref name="TResult"/>. /// </summary> /// <typeparam name="TResult"> /// The type which will be used to parse Json string. /// </typeparam> /// <param name="this"> /// The http content. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="@this"/> is null. /// </exception> /// <exception cref="NotSupportedException"> /// If <paramref name="@this"/> is not "text/plain" or "application/json" content. /// </exception> public static async Task <TResult> ReadAsJsonObject <TResult>(this HttpContent @this) { if ([email protected]() && [email protected]()) { throw new NotSupportedException("ReadAsJsonObject supports only text/plain or application/json content."); } return(JsonConvert.DeserializeObject <TResult>(await @this.ReadAsStringAsync())); }
private static async Task LogContentAsync(StringBuilder log, HttpContent httpContent) { if (httpContent != null) { log.AppendLine(" CONTENT HEADERS: "); LogHeaders(log, httpContent.Headers); if (httpContent.IsPlainText() || httpContent.IsHtmlText() || httpContent.IsJson() || httpContent.IsFormUrlEncoded()) { var content = await httpContent.ReadAsStringAsync(); StorageFolder folder = null; try { folder = (await ApplicationData.Current.LocalFolder.GetFoldersAsync()) .FirstOrDefault(x => string.Equals(x.Name, WebResponseLogs, StringComparison.OrdinalIgnoreCase)); } catch (InvalidOperationException) { // Unit tests does not have package identity. We just ignore them. } if (folder != null) { var fileName = string.Format("{0}.log", Guid.NewGuid()); var file = await folder.CreateFileAsync(fileName); await FileIO.WriteTextAsync(file, content); log.AppendFormat(" CONTENT FILE: {0}", file.Path); } else { log.AppendFormat(" CONTENT:{0}{1}", Environment.NewLine, content.Substring(0, Math.Min(4096, content.Length))); log.AppendLine(); log.AppendFormat(" ENDCONTENT."); log.AppendLine(); } } } else { log.AppendLine(" CONTENT IS NULL."); } }