-
Notifications
You must be signed in to change notification settings - Fork 101
/
HttpClient.cs
88 lines (82 loc) · 3.42 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
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace Info.Blockchain.API
{
public class HttpClient
{
private static string BASE_URI = "https://blockchain.info/";
/// <summary>
/// Performs a GET request on a Blockchain.info API resource.
/// </summary>
/// <param name="resource">Resource path after https://blockchain.info/api/ </param>
/// <param name="parameters">Collection containing request parameters</param>
/// <returns>String response</returns>
public static string Get(string resource, NameValueCollection parameters = null)
{
return OpenURL(resource, "GET", parameters);
}
/// <summary>
/// Performs a POST request on a Blockchain.info API resource.
/// </summary>
/// <param name="resource">Resource path after https://blockchain.info/api/ </param>
/// <param name="parameters">Collection containing request parameters</param>
/// <returns>String response</returns>
public static string Post(string resource, NameValueCollection parameters = null)
{
return OpenURL(resource, "POST", parameters);
}
private static string OpenURL(String resource, string method,
NameValueCollection parameters = null)
{
using (var client = new WebClient())
{
string response = null;
try
{
string query = null;
if (parameters != null && parameters.Count > 0)
{
query = string.Join("&", parameters.AllKeys.Select(key => string.Format("{0}={1}",
HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(parameters[key]))));
}
if (method == "GET")
{
response = client.DownloadString(BASE_URI + resource + "?" + query);
}
else if (method == "POST")
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
byte[] bytes = Encoding.UTF8.GetBytes(query);
byte[] byteResponse = client.UploadData(BASE_URI + resource, bytes);
response = System.Text.Encoding.UTF8.GetString(byteResponse);
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
var httpResponse = e.Response as HttpWebResponse;
if (httpResponse != null && httpResponse.StatusCode != HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
{
throw new APIException(reader.ReadToEnd());
}
}
else
throw e;
}
else
throw e;
}
return response;
}
}
}
}