public async Task<ForumReplyEntity> GetReplyCookies(ForumThreadEntity forumThread) { try { string url = string.Format(Constants.REPLY_BASE, forumThread.ThreadId); WebManager.Result result = await _webManager.DownloadHtml(url); HtmlDocument doc = result.Document; HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray(); HtmlNode formKeyNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("formkey")); HtmlNode formCookieNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("form_cookie")); HtmlNode bookmarkNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("bookmark")); HtmlNode[] textAreaNodes = doc.DocumentNode.Descendants("textarea").ToArray(); HtmlNode textNode = textAreaNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("message")); HtmlNode threadIdNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("threadid")); var threadManager = new ThreadManager(); string htmlThread = await threadManager.GetThreadHtml(doc); var forumReplyEntity = new ForumReplyEntity(); try { string formKey = formKeyNode.GetAttributeValue("value", ""); string formCookie = formCookieNode.GetAttributeValue("value", ""); string quote = WebUtility.HtmlDecode(textNode.InnerText); string threadId = threadIdNode.GetAttributeValue("value", ""); forumReplyEntity.MapThreadInformation(formKey, formCookie, quote, threadId); forumReplyEntity.PreviousPostsRaw = htmlThread; return forumReplyEntity; } catch (Exception) { throw new InvalidOperationException("Could not parse newReply form data."); } } catch (Exception) { return null; } }
public async Task<ForumReplyEntity> GetReplyCookies(ForumThreadEntity forumThread) { try { string url = string.Format(Constants.REPLY_BASE, forumThread.ThreadId); WebManager.Result result = await _webManager.GetData(url); HtmlDocument doc = result.Document; HtmlNode[] formNodes = doc.DocumentNode.Descendants("input").ToArray(); HtmlNode formKeyNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("formkey")); HtmlNode formCookieNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("form_cookie")); HtmlNode bookmarkNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("bookmark")); HtmlNode[] textAreaNodes = doc.DocumentNode.Descendants("textarea").ToArray(); HtmlNode textNode = textAreaNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("message")); HtmlNode threadIdNode = formNodes.FirstOrDefault(node => node.GetAttributeValue("name", "").Equals("threadid")); var threadManager = new ThreadManager(); var forumThreadPosts = new ObservableCollection<ForumPostEntity>(); HtmlNode threadNode = doc.DocumentNode.Descendants("div") .FirstOrDefault(node => node.GetAttributeValue("id", string.Empty).Contains("thread")); foreach ( HtmlNode postNode in threadNode.Descendants("table") .Where(node => node.GetAttributeValue("class", string.Empty).Contains("post"))) { var post = new ForumPostEntity(); post.Parse(postNode); forumThreadPosts.Add(post); } forumThread.ForumPosts = forumThreadPosts; string htmlThread = await HtmlFormater.FormatThreadHtml(forumThread); var forumReplyEntity = new ForumReplyEntity(); try { string formKey = formKeyNode.GetAttributeValue("value", ""); string formCookie = formCookieNode.GetAttributeValue("value", ""); string quote = WebUtility.HtmlDecode(textNode.InnerText); string threadId = threadIdNode.GetAttributeValue("value", ""); forumReplyEntity.MapThreadInformation(formKey, formCookie, quote, threadId); forumReplyEntity.PreviousPostsRaw = htmlThread; return forumReplyEntity; } catch (Exception) { throw new InvalidOperationException("Could not parse newReply form data."); } } catch (Exception) { return null; } }