-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseHtml.cs
66 lines (60 loc) · 1.8 KB
/
ParseHtml.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
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RKstat
{
public class ParseHtml
{
HtmlWeb htmlWeb = new HtmlWeb();
string url;
HttpWebRequest request = null;
CookieContainer cookieContainer = new CookieContainer();
public ParseHtml(string url){
SetDomain(url);
}
public ParseHtml()
{
}
public void SetDomain(string url)
{
this.url = url;
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
}
public void SetCookie(string name, string value, string domain)
{
Cookie cookie = new Cookie(name, value)
{
Domain = new Uri(domain).Host
};
cookieContainer.Add(cookie);
}
public HtmlDocument GetHtml()
{
request.CookieContainer = cookieContainer;
WebResponse webRespon = request.GetResponse();
HttpWebResponse response = (HttpWebResponse)webRespon;
string html;
if (response != null)
{
if (response.StatusCode == HttpStatusCode.OK)
{
Stream stream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
return htmlDocument;
}
}
return null;
}
}
}