bklLiudl
2024-07-23 675b8bcc4a3630d95e3d0b97d933e63442075ecb
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
89
90
91
92
93
94
95
96
97
98
//using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
 
 
namespace Utility.Extra
{
    public class HttpHelper
    {
        private static HttpWebRequest GetHttpWebRequest(string url, Dictionary<string, string> headerDic, string method)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = method;
            if(headerDic != null && headerDic.Count > 0)
            {
                foreach(var h in headerDic)
                {
                    request.Headers.Add(h.Key, h.Value);
                }
            }
            return request;
        }
 
        private static string GetUrlParamString(Dictionary<string, string> paramDic)
        {
            StringBuilder sb = new StringBuilder();
            if(paramDic == null || paramDic.Count() < 1)
            {
                return string.Empty;
            }
            foreach(var d in paramDic)
            {
                if(sb.Length < 1)
                {
                    sb.AppendFormat("{0}={1}", d.Key, d.Value);
                }
                else
                {
                    sb.AppendFormat("&{0}={1}", d.Key, d.Value);
                }
            }
            sb.Insert(0, "?");
            return sb.ToString();
        }
 
        private static string GetResponseString(HttpWebRequest request)
        {
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }
 
        public static string DoPost(string url, string jsonData, Dictionary<string, string> headerDic = null)
        {
            var request = GetHttpWebRequest(url, headerDic, "POST");
            request.ContentType = "application/json";
            request.ContentLength = 0;
            if(!string.IsNullOrWhiteSpace(jsonData))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(jsonData);
                request.ContentLength = buffer.Length;
                try
                {
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                }
                catch(WebException ex)
                {
                    throw ex;
                }
            }
            try
            {
                return GetResponseString(request);
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }
    }
}