领胜LDS 键盘AOI检测项目
xcd
2020-07-02 c866d11e0054a7299076fa53830b96610286ac2c
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
99
100
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.Common.Helper
{
    public class WebApiHelper
    {
        //以毫秒为单位
        readonly int TimeOut = 999999999;
 
        public WebApiHelper(int timeout = 9999999)
        {
            TimeOut = timeout;
        }
 
        public async Task<string> dooPostAsync(string actionUrl, string paras)
        {
            string responseStr = string.Empty;
 
            WebRequest request = WebRequest.Create(actionUrl);
            request.Timeout = TimeOut;
            //request.Headers.Add("Authorization", "SDXK_Service");
            request.Method = "Post";
            request.ContentType = "application/json";
 
            byte[] requestData = System.Text.Encoding.UTF8.GetBytes(paras);
            request.ContentLength = requestData.Length;
 
            Stream newStream = request.GetRequestStream();
            newStream.Write(requestData, 0, requestData.Length);
            newStream.Close();
 
            var response = await request.GetResponseAsync();
            Stream ReceiveStream = response.GetResponseStream();
            using (StreamReader stream = new StreamReader(ReceiveStream, Encoding.UTF8))
            {
                responseStr = stream.ReadToEnd();
            }
 
            return responseStr;
        }
 
        /// <summary></summary>
        /// <param name="actionUrl"></param>
        /// <param name="content">  
        /// var content = new FormUrlEncodedContent(new Dictionary<string, string>()
        ///{
        ///   { "","AEADDE34"}
        ///});
        ///</param>
        /// <returns></returns>
        public string dooPost(string actionUrl, HttpContent content)
        {
            var httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, TimeOut / 1000);
            //httpClient.DefaultRequestHeaders.Add("Authorization", "SDXK_Service");
 
            return httpClient.PostAsync(actionUrl, content)
            .Result.Content.ReadAsStringAsync().Result;
        }
 
        public string dooPost(string actionUrl, string paras)
        {
            string responseStr = string.Empty;
 
            WebRequest request = WebRequest.Create(actionUrl);
            request.Timeout = TimeOut;
            //request.Headers.Add("Authorization", "SDXK_Service");
            request.Method = "Post";
            request.ContentType = "application/json";
 
            byte[] requestData = System.Text.Encoding.UTF8.GetBytes(paras);
            request.ContentLength = requestData.Length;
 
            Stream newStream = request.GetRequestStream();
            newStream.Write(requestData, 0, requestData.Length);
            newStream.Close();
 
            var response = request.GetResponse();
            Stream ReceiveStream = response.GetResponseStream();
            using (StreamReader stream = new StreamReader(ReceiveStream, Encoding.UTF8))
            {
                responseStr = stream.ReadToEnd();
            }
 
            return responseStr;
        }
 
        public string dooGet(string actionUrl)
        {
            var httpClient = new HttpClient();
            httpClient.Timeout = new TimeSpan(0, 0, TimeOut / 1000);
            return httpClient.GetAsync(actionUrl).Result.Content.ReadAsStringAsync().Result;
        }
    }
}