forked from taylorfinnell/GoogleMusicAPI2.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleCookieManager.cs
71 lines (61 loc) · 1.8 KB
/
GoogleCookieManager.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Byteopia.Music.GoogleMusicAPI
{
public class GoogleCookieManager
{
public static String URI = "https://play.google.com/music/";
private CookieContainer cookieContainer;
[DataMember(Name = "Cookies")]
public IEnumerable<Cookie> Cookies
{
get
{
return GetCookiesList();
}
}
public GoogleCookieManager()
{
cookieContainer = new CookieContainer();
}
public bool HandleResponse(HttpResponseMessage msg)
{
bool cookiesChanged = false;
IEnumerable<String> cookies;
if (msg.Headers.TryGetValues("Set-Cookie", out cookies))
{
foreach (String cookie in cookies)
{
cookieContainer.SetCookies(new Uri(URI), cookie);
cookiesChanged = true;
}
}
return cookiesChanged;
}
public void SetCookiesFromList(List<Cookie> cookies)
{
if (cookies == null) return;
foreach (Cookie c in cookies)
cookieContainer.Add(new Uri(URI), c);
}
public String GetCookies()
{
return cookieContainer.GetCookieHeader(new Uri(URI));
}
public List<Cookie> GetCookiesList()
{
List<Cookie> cookies = new List<Cookie>();
foreach (Cookie cookie in cookieContainer.GetCookies(new Uri(URI)))
{
cookies.Add(cookie);
}
return cookies;
}
}
}