-
Notifications
You must be signed in to change notification settings - Fork 10
/
HttpUtil.cs
124 lines (116 loc) · 4.54 KB
/
HttpUtil.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
namespace jjget
{
class HttpUtil
{
string encoding = "utf-8";
WebProxy proxy = null;
public string cookiestr = "";
public void setProxy(WebProxy proxy)
{
this.proxy = proxy;
}
public void setEncoding(string encoding)
{
this.encoding = encoding;
}
private Stream getStream(HttpWebResponse resp, int timeout)
{
Stream stream;
switch (resp.ContentEncoding.ToUpperInvariant())
{
case "GZIP":
stream = new GZipStream(resp.GetResponseStream(), CompressionMode.Decompress);
break;
case "DEFLATE":
stream = new DeflateStream(resp.GetResponseStream(), CompressionMode.Decompress);
break;
default:
stream = resp.GetResponseStream();
stream.ReadTimeout = timeout;
break;
}
return stream;
}
private Stream get(string url, string referer, string accept, bool ignoreSetCookie)
{
HttpWebRequest request;
Encoding encoding = Encoding.Default;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.KeepAlive = true;
request.Method = "get";
request.Timeout = 45000; // 45s
//request.CookieContainer = new CookieContainer();
if (proxy != null)
request.Proxy = proxy;
request.Headers.Add("Accept-encoding:gzip,deflate");
if (cookiestr != "")
request.Headers.Add("Cookie:" + cookiestr);
if (referer != null)
request.Referer = referer;
if (accept == null)
accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Accept = accept;
request.UserAgent = "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; JJGET) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.7.5.418 U3/0.8.0 Mobile Safari/533.1";
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
string set_cookie = resp.Headers["set-cookie"];
if (set_cookie != null && set_cookie.IndexOf(",") != -1 && !ignoreSetCookie)
{
cookiestr = "testcookie=yes;";
foreach (string l in set_cookie.Split(','))
{
if (l.Split(';')[0].IndexOf("=") == -1 || l.Split(';')[0].IndexOf("deleted") != -1) continue;
cookiestr += l.Split(';')[0] + ";";
}
}
return getStream(resp, 10000);
}
public string Get(string url)
{
return Get(url, null, false);
}
public string Get(string url, string referer)
{
return Get(url, referer, false);
}
public string Get(string url, string referer, bool ignoreSetCookie) {
Stream respStream = get(url, referer, null, ignoreSetCookie);
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(this.encoding)))
{
return reader.ReadToEnd();
}
}
public byte[] GetBinary(string url, string accept)
{
Stream respStream = get(url, null, accept, false);
try
{
int bytesBuffer = 1024;
byte[] buffer = new byte[bytesBuffer];
using (MemoryStream ms = new MemoryStream())
{
int readBytes;
while ((readBytes = respStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, readBytes);
}
return ms.ToArray();
}
}
catch (Exception)
{
return new byte[0];
}
}
public static int getTimeStamp(){
return (int)(DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1))).TotalSeconds;
}
}
}