Skip to content

Commit a259574

Browse files
committed
CookiesStorage: ToBytes() and FromBytes(byte[])
1 parent e85b22c commit a259574

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Leaf.xNet/~Http/CookieStorage.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ public bool Contains(string url, string cookieName)
382382
return Contains(new Uri(url), cookieName);
383383
}
384384

385+
386+
#region Load / Save: File
387+
385388
/// <summary>
386389
/// Сохраняет куки в файл.
387390
/// <remarks>Рекомендуется расширение .jar.</remarks>
@@ -412,5 +415,41 @@ public static CookieStorage LoadFromFile(string filePath)
412415
using (var fs = new FileStream(filePath, FileMode.Open))
413416
return (CookieStorage)Bf.Deserialize(fs);
414417
}
418+
419+
#endregion
420+
421+
422+
#region Save / Load: Bytes
423+
424+
/// <summary>
425+
/// Сохраняет куки в массив байт.
426+
/// </summary>
427+
// ReSharper disable once UnusedMember.Global
428+
public byte[] ToBytes()
429+
{
430+
byte[] r;
431+
432+
using (var ms = new MemoryStream())
433+
{
434+
Bf.Serialize(ms, this);
435+
r = ms.ToArray();
436+
}
437+
438+
return r;
439+
}
440+
441+
/// <summary>
442+
/// Загружает <see cref="CookieStorage"/> из массива байт.
443+
/// </summary>
444+
/// <param name="bytes">Массив байт</param>
445+
/// <returns>Вернет <see cref="CookieStorage"/>, который задается в свойстве <see cref="HttpRequest"/> Cookies.</returns>
446+
// ReSharper disable once UnusedMember.Global
447+
public static CookieStorage FromBytes(byte[] bytes)
448+
{
449+
using (var ms = new MemoryStream(bytes))
450+
return (CookieStorage)Bf.Deserialize(ms);
451+
}
452+
453+
#endregion
415454
}
416455
}

0 commit comments

Comments
 (0)