static async void DownloadPageAsync() { // ... Target page. //string page = "https://jsonplaceholder.typicode.com/posts"; //string page = "http://www.mocky.io/v2/5d447b6b2f000080291796ce"; string page = "http://localhost:3000/api/todo"; // ... Use HttpClient. using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); using (HttpResponseMessage response = await client.GetAsync(page)) using (HttpContent content = response.Content) { // ... Read the string. string result = await content.ReadAsStringAsync(); // ... Display the result. if (result != null && result.Length >= 50) { Console.WriteLine(result); dynamic json = JsonConvert.DeserializeObject(result); Newtonsoft.Json.Linq.JArray jarr = json; OnePost v = jarr[0].ToObject <OnePost>(); var va = jarr.ToObject <OnePost[]>(); } } } }
public OnePost GetPost(string BoardName, string Filename) { log.Info($"GetPost[BoardName]:{BoardName}|[Filename]:{Filename}"); string text = string.Empty; string url = $"https://www.ptt.cc/bbs/{BoardName}/{Filename}.html"; MyWebClient client = new MyWebClient(); client.Encoding = Encoding.UTF8; // 設定Webclient.Encoding OnePost post = new OnePost(); post.Href = url; post.Board = BoardName; string html = "未知"; try { html = client.DownloadString(url); List <string> iteamList = new List <string>(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlDocument doctag = new HtmlDocument(); var metaTags = doc.DocumentNode.SelectNodes("//div[@id='main-content']"); if (metaTags != null) { foreach (var tag in metaTags) { if (tag.Attributes["class"] != null) { if (tag.Attributes["class"].Value == "bbs-screen bbs-content") { ////class="title" class="author" class="date" doctag.LoadHtml(tag.OuterHtml); HtmlNodeCollection metaline = doctag.DocumentNode.SelectNodes("//div[@class='article-metaline']"); foreach (var dtag in metaline) { if (dtag == null) { continue; } switch (dtag.ChildNodes[0].InnerText) { case "作者": string strAuthor = dtag.ChildNodes[1].InnerText; string[] sA = strAuthor.Split('('); post.Author = sA[0].Trim(); if (sA.Length > 1) { post.Nickname = sA[1].Replace(")", ""); } continue; case "標題": post.Title = dtag.ChildNodes[1].InnerText; continue; case "時間": post.Date = dtag.ChildNodes[1].InnerText; continue; default: continue; } } post.Comments = new List <Comment>(); metaline = doctag.DocumentNode.SelectNodes("//div[@class='push']"); if (metaline != null) { foreach (var dtag in metaline) { if (dtag == null) { continue; } Comment push = new Comment(); push.Tag = dtag.ChildNodes[0].InnerText.Trim(); push.Userid = dtag.ChildNodes[1].InnerText; push.Content = dtag.ChildNodes[2].InnerText; push.IPdatetime = dtag.ChildNodes[3].InnerText; post.Comments.Add(push); } } StringBuilder sb = new StringBuilder(); string[] Content = WebUtility.HtmlDecode(tag.InnerText).Split('\n'); foreach (var str in Content.Skip(1)) { if (str.IndexOf("※ 發信站") > -1) { break; } if (str.IndexOf("※ 編輯") > -1) { break; } sb.AppendLine(str); } post.Content = sb.ToString(); } } } } text = Newtonsoft.Json.JsonConvert.SerializeObject(post); //Console.WriteLine(text); } catch (Exception ex) { //Console.WriteLine(ex.Message); log.Debug($"GetPost[Exception]:{ex.Message}"); log.Debug(ex.StackTrace); } return(post); }