This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
forked from NooodyFR/WidevineClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtil.cs
99 lines (83 loc) · 3.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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
namespace WidevineClient
{
class HttpUtil
{
public static HttpClient Client { get; set; } = new HttpClient(new HttpClientHandler
{
AllowAutoRedirect = true,
//Proxy = null
});
public static byte[] PostData(string URL, Dictionary<string, string> headers, string postData)
{
var mediaType = postData.StartsWith("{") ? "application/json" : "application/x-www-form-urlencoded";
StringContent content = new StringContent(postData, Encoding.UTF8, mediaType);
//ByteArrayContent content = new ByteArrayContent(postData);
HttpResponseMessage response = Post(URL, headers, content);
byte[] bytes = response.Content.ReadAsByteArrayAsync().Result;
return bytes;
}
public static byte[] PostData(string URL, Dictionary<string, string> headers, byte[] postData)
{
ByteArrayContent content = new ByteArrayContent(postData);
HttpResponseMessage response = Post(URL, headers, content);
byte[] bytes = response.Content.ReadAsByteArrayAsync().Result;
return bytes;
}
public static byte[] PostData(string URL, Dictionary<string, string> headers, Dictionary<string, string> postData)
{
FormUrlEncodedContent content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = Post(URL, headers, content);
byte[] bytes = response.Content.ReadAsByteArrayAsync().Result;
return bytes;
}
public static string GetWebSource(string URL, Dictionary<string, string> headers = null)
{
HttpResponseMessage response = Get(URL, headers);
byte[] bytes = response.Content.ReadAsByteArrayAsync().Result;
return Encoding.UTF8.GetString(bytes);
}
public static byte[] GetBinary(string URL, Dictionary<string, string> headers = null)
{
HttpResponseMessage response = Get(URL, headers);
byte[] bytes = response.Content.ReadAsByteArrayAsync().Result;
return bytes;
}
public static string GetString(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
static HttpResponseMessage Get(string URL, Dictionary<string, string> headers = null)
{
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(URL),
Method = HttpMethod.Get
};
if (headers != null)
foreach (KeyValuePair<string, string> header in headers)
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
return Send(request);
}
static HttpResponseMessage Post(string URL, Dictionary<string, string> headers, HttpContent content)
{
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(URL),
Method = HttpMethod.Post,
Content = content
};
if (headers != null)
foreach (KeyValuePair<string, string> header in headers)
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
return Send(request);
}
static HttpResponseMessage Send(HttpRequestMessage request)
{
return Client.SendAsync(request).Result;
}
}
}