-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpMethods.cs
45 lines (39 loc) · 1.6 KB
/
HttpMethods.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
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
public class HttpMethods {
HttpClient client;
/// <summary>
/// HttpClient is disposible. However: https://stackoverflow.com/a/15708633/10008842
/// </summary>
public HttpClient Client {
get {
if (client == null) client = new HttpClient();
return client;
}
set => client = value;
}
public async Task<HttpResponseMessage> Post(string url, Dictionary<string, string> values) {
var content = new FormUrlEncodedContent(values);
return await Client.PostAsync(url, content);
}
public async Task<HttpResponseMessage> Post(string url, string json) {
return await Client.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"));
//var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//httpWebRequest.ContentType = "application/json";
//httpWebRequest.Method = "POST";
//using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
// streamWriter.Write(json);
//}
//var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//using var streamReader = new StreamReader(httpResponse.GetResponseStream());
//return streamReader.ReadToEnd();
}
public async Task<HttpResponseMessage> Post(string url, object anonymous_type) {
return await Post(url, new JavaScriptSerializer().Serialize(anonymous_type));
}
}