forked from InstaSharp/InstaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClient.cs
82 lines (67 loc) · 2.61 KB
/
HttpClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections.Specialized;
namespace InstaSharp {
public static class HttpClient {
public static string GET(string uri) {
try {
var request = HttpWebRequest.Create(uri);
request.Method = "GET";
return ReadResponse(request.GetResponse().GetResponseStream());
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
public static string POST(string url) {
return POST(url, new Dictionary<string, string>());
}
public static string POST(string url, IDictionary<string, string> args) {
try {
NameValueCollection parameters = new NameValueCollection();
foreach (var arg in args) {
parameters.Add(arg.Key, arg.Value);
}
WebClient client = new WebClient();
var result = client.UploadValues(url, "POST", parameters);
return Encoding.Default.GetString(result);
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
public static string DELETE(string uri) {
var request = HttpWebRequest.Create(uri);
request.Method = "DELETE";
return DELETE(uri, new Dictionary<string, string>());
// return ReadResponse(request.GetResponse().GetResponseStream());
}
public static string DELETE(string uri, IDictionary<string, string> args) {
try {
NameValueCollection parameters = new NameValueCollection();
foreach (var arg in args) {
parameters.Add(arg.Key, arg.Value);
}
WebClient client = new WebClient();
var result = client.UploadValues(uri, "DELETE", parameters);
return Encoding.Default.GetString(result);
}
catch (WebException ex) {
return ReadResponse(ex.Response.GetResponseStream());
}
}
private static string ReadResponse(Stream response) {
StreamReader reader = new StreamReader(response);
string line;
StringBuilder result = new StringBuilder();
while ((line = reader.ReadLine()) != null) {
result.Append(line);
}
return result.ToString();
}
}
}