This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
HttpUtility.cs
119 lines (106 loc) · 4.12 KB
/
HttpUtility.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
using System;
using System.IO;
using System.Net;
using System.Text;
namespace RedditSaveTransfer
{
public static class HttpUtility
{
/// <summary>
/// Sends a POST to the specified server
/// </summary>
/// <param name="uri">URI to send</param>
/// <param name="data">Additional data</param>
/// <param name="cookie">The cookie to associate with the request</param>
/// <returns>Server response</returns>
public static string SendPost(string uri, string data, CookieContainer cookie)
{
var request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookie;
request.UserAgent = Common.UserAgent;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
//Grab the default proxy from IE Internet Settings
var proxy = WebRequest.GetSystemWebProxy();
var wp = new WebProxy
{
Credentials = proxy.Credentials,
Address = proxy.GetProxy(request.RequestUri)
};
// If there is no proxy set up, it will end up being the same as the request Uri
if (!wp.Address.Equals(request.RequestUri))
{
request.Proxy = wp;
}
//Encode the data
using (var writeStream = request.GetRequestStream())
{
var encoding = new UTF8Encoding();
var bytes = encoding.GetBytes(data);
writeStream.Write(bytes, 0, bytes.Length);
}
//Send the request and get the server's response
var result = string.Empty;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
return result;
}
/// <summary>
/// Sends a GET to the specified server
/// </summary>
/// <param name="uri">URI to send</param>
/// <param name="cookie">The cookie to associate with the request</param>
/// <returns>Server response</returns>
public static string SendGet(string uri, CookieContainer cookie)
{
HttpWebRequest request = null;
request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookie;
request.UserAgent = Common.UserAgent;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
//Grab the default proxy from IE Internet Settings
var proxy = WebRequest.GetSystemWebProxy();
var wp = new WebProxy
{
Credentials = proxy.Credentials,
Address = proxy.GetProxy(request.RequestUri)
};
// If there is no proxy set up, it will end up being the same as the request Uri
if (!wp.Address.Equals(request.RequestUri))
{
request.Proxy = wp;
}
var result = string.Empty;
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
using (var readStream = new StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error sending GET message: " + e.Message);
}
return result;
}
}
}