Salut tout le monde!
Je suis sur un sujet d'étude d'un prof est il m'a demander de terminer une partie d'un script C# qu'il a trouver sur le net pour faire un exemple des connections sur un forum a partir de Dotnet Framework.
Alors le problème se trouve au niveau des cookies.
Le problème est que VS2008 me donne une erreur
System.InvalidOperationException
malgré que les démarches sont bonne.

Pourriez vous m'aider à trouver une solution en me mettant sur une piste ?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
 
 
public class BayW : ILeechSite
	{
		public string Name { get; private set; }
		public string HomePage { get; private set; }
		public string LoginPage { get; private set; }
		public string RegisterPage { get; private set; }
		public bool IsLoggedIn { get; private set; }
		public CookieCollection Cookies { get; private set; }
		public ForumBrand Brand { get; private set; }
		public Dictionary<string, string> Sections { get; private set; }
 
		public BayW()
		{
			// Init our properties
			this.Name = "BayW";
			this.HomePage = "http://bayw.org/";
			this.LoginPage = "http://bayw.org/login.php";
			this.RegisterPage = "http://bayw.org/profile.php?mode=register";
			this.IsLoggedIn = false;
			this.Sections = new Dictionary<string, string>();
			this.Cookies = new CookieCollection();
			this.Brand = ForumBrand.phpBB;
 
			// Setup our sections
			this.Sections.Add("Applications", "1");
			this.Sections.Add("All In One AIO", "23");
			this.Sections.Add("Games", "2");
			this.Sections.Add("Console Games", "22");
			this.Sections.Add("Movies", "3");
			this.Sections.Add("TV Shows", "20");
			this.Sections.Add("Music", "4");
			this.Sections.Add("Music Videos", "21");
			this.Sections.Add("eBooks & Tutorials", "5");
			this.Sections.Add("Web Developments", "6");
			this.Sections.Add("Non-Windows Warez", "7");
			//this.Sections.Add("", "");
		}
 
		public ForumThread GetThreadContent(string threadUrl)
		{
			HtmlDocument doc;
			string data, title, content, quoteLink;
 
			// Set the properties
			data = Leech.HttpGET(threadUrl, this.Cookies);
			doc = new HtmlDocument();
			doc.LoadHtml(data);
			quoteLink = title = content = "";
 
			// Get the title
			title = doc.DocumentNode.SelectNodes("//title")[0].InnerText.Replace("BayW.org :: ", "").Trim();
 
			// Find the link to the quote page so we can grab the bbcode
			foreach (HtmlNode n in doc.DocumentNode.SelectNodes("//img[@alt='Reply with quote']"))
			{
				if (n.ParentNode.GetAttributeValue("href", "").StartsWith("posting.php?mode=quote"))
				{
					quoteLink = HttpUtility.HtmlDecode(n.ParentNode.GetAttributeValue("href", ""));
					break;
				}
			}
 
			// Get the contents of this page and load the html.
			data = Leech.HttpGET(this.HomePage + quoteLink, this.Cookies);
			doc.LoadHtml(data);// System.Windows.Forms.MessageBox.Show(this.HomePage + quoteLink);
 
			try
			{
				// Get the thread content
				content = doc.DocumentNode.SelectNodes("//textarea[@name='message']")[0].InnerText;
 
				// Remove the quote
				content = content.Substring(content.IndexOf(']') + 1);
				content = content.Substring(0, content.Length - "[/quote]".Length);
			}
			catch (NullReferenceException)
			{
				// Thread was most likely closed
				return new ForumThread(HttpUtility.HtmlDecode(title), "", threadUrl);
			}
 
			// Return data
			return new ForumThread(HttpUtility.HtmlDecode(title), HttpUtility.HtmlDecode(content), threadUrl);
		}
 
		public string[] GetThreadUrls(int start, string section)
		{
			HtmlDocument doc;
			HtmlNode parant;
			List<string> threadLinks;
			string data;
 
			// Set the properties
			data = Leech.HttpGET(this.HomePage + "viewforum.php?f=" +
				section + "&topicdays=0&start=" + (start * 50),
				this.Cookies);
			doc = new HtmlDocument();
			threadLinks = new List<string>();
			doc.LoadHtml(data);
 
			// Hunt for links to threads
			foreach (HtmlNode n in doc.DocumentNode.SelectNodes("//a[@class='topictitle']"))
			{
				// Make sure were dealing with a valid link
				if (n.GetAttributeValue("href", "").StartsWith("viewtopic.php?t="))
				{
					// And we dont want to be leeching the announcements ^^
					try
					{
						parant = n.ParentNode.ParentNode;
 
						if (!parant.SelectNodes("td/a/img")[0].GetAttributeValue("src", "").Contains("announce") && !parant.SelectNodes("td/a/img")[0].GetAttributeValue("src", "").Contains("sticky"))
						{
							// Got one, add to the list !
							threadLinks.Add(this.HomePage + n.GetAttributeValue("href", ""));
						}
					}
					catch { }
				}
			}
 
			return threadLinks.ToArray();
		}
 
		public bool Login(string username, string password)
		{
			byte[] data;
			string result;
			string decoded;
			HttpWebRequest request;
			HttpWebResponse response;
			Stream stream;
			StreamReader streamReader;
 
            data = Encoding.ASCII.GetBytes(
				"autologin=1&login=true&username=" +
				HttpUtility.UrlEncode(username) +
				"&password=" +
				HttpUtility.UrlEncode(password)
			);
 
			request = (HttpWebRequest)WebRequest.Create(this.LoginPage);
			request.CookieContainer = new CookieContainer(128);
			request.Timeout = Leech.GlobalTimeout;
			request.Method = "POST";
			request.ContentType = "application/x-www-form-urlencoded";
			request.AllowAutoRedirect = false;
			request.ContentLength = data.Length;
 
			stream = request.GetRequestStream();
			stream.Write(data, 0, data.Length);
			stream.Close();
 
			response = (HttpWebResponse)request.GetResponse();
 
			// FAILLLL =)
			if (response == null) return false;
 
			streamReader = new StreamReader(response.GetResponseStream());
			result = streamReader.ReadToEnd().Trim();
			streamReader.Close();
 
			// Did we login??
			foreach (Cookie c in response.Cookies)
			{
                response.Cookies.Add(c);
            }
 
			// If we get to this point we did not login
			return false;
		}
 
		public override string ToString()
		{
			return this.Name;
		}
	}
}