-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathNativeCookieHandler.cs
More file actions
50 lines (42 loc) · 1.33 KB
/
NativeCookieHandler.cs
File metadata and controls
50 lines (42 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Java.Net;
namespace ModernHttpClient
{
public class NativeCookieHandler
{
readonly CookieManager cookieManager = new CookieManager();
public NativeCookieHandler()
{
CookieHandler.Default = cookieManager; //set cookie manager if using NativeCookieHandler
}
public void SetCookies(IEnumerable<Cookie> cookies)
{
foreach (var nc in cookies.Select(ToNativeCookie)) {
cookieManager.CookieStore.Add(new URI(nc.Domain), nc);
}
}
public IReadOnlyList<Cookie> Cookies {
get {
return cookieManager.CookieStore.Cookies
.Select(ToNetCookie)
.ToList();
}
}
static HttpCookie ToNativeCookie(Cookie cookie)
{
var nc = new HttpCookie(cookie.Name, cookie.Value);
nc.Domain = cookie.Domain;
nc.Path = cookie.Path;
nc.Secure = cookie.Secure;
return nc;
}
static Cookie ToNetCookie(HttpCookie cookie)
{
var nc = new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
nc.Secure = cookie.Secure;
return nc;
}
}
}