-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
43 lines (37 loc) · 1.38 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.Web.Http;
namespace ZonalAPI
{
public class Utils
{
public static async Task<string> PostHTTPString(Uri requestUri, string request)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "application/json");
Dictionary<string, string> items = new Dictionary<string, string>();
items.Add("request", request);
HttpFormUrlEncodedContent content = new HttpFormUrlEncodedContent(items);
HttpResponseMessage resp = await client.PostAsync(requestUri, content);
if (resp.IsSuccessStatusCode)
return await resp.Content.ReadAsStringAsync();
else
return string.Empty;
}
}
public static string SerializeJson(object instance)
{
using (MemoryStream _Stream = new MemoryStream())
{
var _Serializer = new DataContractJsonSerializer(instance.GetType());
_Serializer.WriteObject(_Stream, instance);
return Encoding.UTF8.GetString(_Stream.ToArray(), 0, (int)_Stream.Length);
}
}
}
}