-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProHttpClient.cs
100 lines (90 loc) · 3.36 KB
/
ProHttpClient.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace TrixieBot
{
public class ProHttpClient : HttpClient
{
public ProHttpClient()
{
Timeout = new TimeSpan(0, 0, 30);
ReferrerUri = "https://duckduckgo.com";
AuthorizationHeader = string.Empty;
DefaultRequestHeaders.TryAddWithoutValidation("Accept", "*/*");
DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "en-US,en;q=0.5");
DefaultRequestHeaders.TryAddWithoutValidation("Connection", "keep-alive");
DefaultRequestHeaders.TryAddWithoutValidation("DNT", "1");
DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/56.0");
CustomHeaders = new Dictionary<string, string>();
}
public string AuthorizationHeader { get; set; }
public string BingHeader { get; set; }
public string ReferrerUri { get; set; }
public Dictionary<string,string> CustomHeaders {get; set;}
public void AddHeader (string name, string value) {
CustomHeaders.Add(name,value);
DefaultRequestHeaders.TryAddWithoutValidation(name, value);
}
public async Task<string> DownloadString(string uri)
{
BuildHeaders();
try{
var response = await GetStringAsync(uri).ConfigureAwait(false);
return response;
}
catch (System.Net.Http.HttpRequestException ex){
Console.Write(ex.ToString());
return ex.Message;
}
finally{
CleanHeaders();
}
}
public async Task<Stream> DownloadData(string uri)
{
BuildHeaders();
try{
var response = await GetStreamAsync(uri).ConfigureAwait(false);
return response;
}
catch (System.Net.Http.HttpRequestException ex){
Console.Write(ex.ToString());
throw;
}
finally{
CleanHeaders();
}
}
private void BuildHeaders()
{
if(ReferrerUri != string.Empty)
{
DefaultRequestHeaders.Referrer = new Uri(ReferrerUri);
}
if (AuthorizationHeader != string.Empty)
{
DefaultRequestHeaders.TryAddWithoutValidation("Authorization", AuthorizationHeader);
}
if (BingHeader != string.Empty)
{
DefaultRequestHeaders.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", BingHeader);
}
foreach(var header in CustomHeaders){
DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
Console.WriteLine(header.Key + ":" + header.Value);
}
}
private void CleanHeaders()
{
ReferrerUri = "https://duckduckgo.com";
AuthorizationHeader = string.Empty;
DefaultRequestHeaders.Remove("Authorization");
foreach(var header in CustomHeaders){
DefaultRequestHeaders.Remove(header.Key);
}
CustomHeaders.Clear();
}
}
}