Skip to content

Commit

Permalink
CookiesStorage: ToBytes() and FromBytes(byte[])
Browse files Browse the repository at this point in the history
  • Loading branch information
grandsilence committed Dec 27, 2019
1 parent e85b22c commit a259574
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Leaf.xNet/~Http/CookieStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ public bool Contains(string url, string cookieName)
return Contains(new Uri(url), cookieName);
}


#region Load / Save: File

/// <summary>
/// Сохраняет куки в файл.
/// <remarks>Рекомендуется расширение .jar.</remarks>
Expand Down Expand Up @@ -412,5 +415,41 @@ public static CookieStorage LoadFromFile(string filePath)
using (var fs = new FileStream(filePath, FileMode.Open))
return (CookieStorage)Bf.Deserialize(fs);
}

#endregion


#region Save / Load: Bytes

/// <summary>
/// Сохраняет куки в массив байт.
/// </summary>
// ReSharper disable once UnusedMember.Global
public byte[] ToBytes()
{
byte[] r;

using (var ms = new MemoryStream())
{
Bf.Serialize(ms, this);
r = ms.ToArray();
}

return r;
}

/// <summary>
/// Загружает <see cref="CookieStorage"/> из массива байт.
/// </summary>
/// <param name="bytes">Массив байт</param>
/// <returns>Вернет <see cref="CookieStorage"/>, который задается в свойстве <see cref="HttpRequest"/> Cookies.</returns>
// ReSharper disable once UnusedMember.Global
public static CookieStorage FromBytes(byte[] bytes)
{
using (var ms = new MemoryStream(bytes))
return (CookieStorage)Bf.Deserialize(ms);
}

#endregion
}
}

0 comments on commit a259574

Please sign in to comment.