summaryrefslogtreecommitdiff
path: root/Decompiler/Gestor.Application.Helpers/HttpHelper.cs
blob: d552c843ecfa4714fae94c40d42d3a557eafdd47 (plain)
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Gestor.Application.Helpers;

public static class HttpHelper
{
	public static Uri AddQuery<T>(this Uri uri, string name, T value)
	{
		UriBuilder uriBuilder = new UriBuilder(uri);
		NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(uri.Query);
		nameValueCollection.Add(name, value?.ToString());
		uriBuilder.Query = nameValueCollection.ToString();
		return uriBuilder.Uri;
	}

	public static Uri SetQuery(this Uri uri, string name, string value, bool escapeValue = true)
	{
		UriBuilder uriBuilder = new UriBuilder(uri);
		Dictionary<string, string> dictionary = uri.ParseQueryString();
		string value2 = (escapeValue ? Uri.EscapeDataString(value) : value);
		if (dictionary.ContainsKey(name))
		{
			dictionary.Remove(name);
		}
		dictionary.Add(name, value2);
		List<string> values = dictionary.Select((KeyValuePair<string, string> x) => x.Key + "=" + x.Value).ToList();
		string query = string.Join("&", values);
		uriBuilder.Query = query;
		return uriBuilder.Uri;
	}

	public static Dictionary<string, string> ParseQueryString(this Uri uri)
	{
		int startIndex = uri.Query.IndexOf('?') + 1;
		return (from o in uri.Query.Substring(startIndex).Split(new char[1] { '&' })
			select o.Split(new char[1] { '=' }) into items
			where items.Length == 2
			select items).ToDictionary((string[] pair) => pair[0], (string[] pair) => pair[1]);
	}

	public static Uri ToUri(this string uri)
	{
		Uri.TryCreate(uri, UriKind.Absolute, out Uri result);
		return result;
	}

	public static StringContent ToHttpContent<T>(this T content, Encoding encoding = null, string mediaType = "application/json")
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		return new StringContent(JsonConvert.SerializeObject((object)content), Encoding.UTF8, mediaType);
	}

	public static JObject ToJObject(this string jsonString)
	{
		try
		{
			return JObject.Parse(jsonString);
		}
		catch (Exception)
		{
			return null;
		}
	}

	public static JObject ToJObject(this HttpContent content)
	{
		return content.ReadAsStringAsync().Result.ToJObject();
	}

	public static string ToBase64BasicEncode(this string value)
	{
		return "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
	}
}