wangxinhui
2024-12-26 78b99e5348592a29ca1393a5e13db619cc4eba56
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using WIDESEA_Common.Tools;
 
namespace WIDESEA_Core.Utilities
{
    public class WebResponse
    {
        public string message { get; set; }
 
        public bool status { get; set; }
 
        public object data { get; set; }
 
        public void Success(object data, string msg = "")
        {
            this.data = data;
            this.message = msg;
            this.status = true;
        }
 
        public WebResponse Success(string msg = "")
        {
            this.message = msg;
            this.status = true;
            return this;
        }
 
        public WebResponse Error(string msg = "")
        {
            this.message = msg;
            this.status = false;
            return this;
        }
    }
 
    public class ToWIFI
    {
 
        //private static Logger logger = Logger.getLogger();
        /// <summary>
        /// 带参数的Post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string PostByPara(string Url, Dictionary<string, string> jsonParas)
        {
            //logger.DebugStart("HttpHelper.PostByPara");
            //logger.DebugStart("Url=" + Url + ",jsonParas=" + jsonParas);
           // WriteLog.GetLog("PostByPara").Write("PostByPara接口调用开始" + Url + jsonParas + DateTime.Now, "PostByPara");
            if (Url != string.Empty && jsonParas != null)
            {
                try
                {
                    string strURL = Url;
                    HttpWebRequest request;
                    request = (HttpWebRequest)WebRequest.Create(strURL);
                    request.Method = "POST";
                    request.ContentType = "application/json;charset=UTF-8";
                    request.CookieContainer = null;
                    request.Timeout = 30000;
                    string paraUrlCoded = JsonConvert.SerializeObject(jsonParas);
                    byte[] payload;
                    payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                    request.ContentLength = payload.Length;
                    Stream writer = request.GetRequestStream();
                    writer.Write(payload, 0, payload.Length);
                    writer.Close();
                    System.Net.HttpWebResponse response;
                    response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.Stream s;
                    s = response.GetResponseStream();
                    string StrDate = "";
                    string strValue = "";
                    StreamReader Reader = new StreamReader(s, Encoding.UTF8);
                    while ((StrDate = Reader.ReadLine()) != null)
                    {
                        strValue += StrDate + "\r\n";
                    }
                    Reader.Close();
                    response.Close();
                    //logger.DebugEnd("result=" + strValue);
                    //logger.DebugEnd("HttpHelper.PostByPara");
                    //WriteLog.GetLog("PostByPara").Write("PostByPara接口调用结束" + Url + strValue + DateTime.Now, "PostByPara");
                    return strValue;
 
                }
                catch (Exception ex)
                {
                   // WriteLog.GetLog("PostByPara").Write($"PostByPara接口调用失败:{Url},{ex.Message}", "PostByPara");
                    //logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100001 + " -> " + ErrorCode.Common.cx100001.GetDescription());
                    //logger.Error("HttpHelper.PostByPara -> " + ex.ToString());
                }
            }
            else
            {
                //logger.Error("HttpHelper.PostByPara -> " + ErrorCode.Common.cx100002+ " -> " + ErrorCode.Common.cx100002.GetDescription());
 
            }
            //logger.DebugEnd("result=null");
            //logger.DebugEnd("HttpHelper.PostByPara");
            return null;
        }
 
        /// <summary>
        /// post请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parm">参数</param>
        /// <returns></returns>
        public static WebResponse Post(string url, object parm)
        {
            WebResponse res = new WebResponse();
            HttpWebResponse response = null;
            StreamReader resultReader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = 5000;
                request.Method = "POST";
                request.ContentType = "application/json; charset=UTF-8";
                byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(parm));
                request.ContentLength = data.Length;
                using (Stream newStream = request.GetRequestStream())
                {
                    newStream.Write(data, 0, data.Length);
                };
 
                response = (HttpWebResponse)request.GetResponse();
                Stream webStream = response.GetResponseStream();
                if (webStream == null)
                {
                    throw new Exception("Network error");
                }
                int statsCode = (int)response.StatusCode;
 
                resultReader = new StreamReader(webStream, Encoding.UTF8);
                string responseContent = resultReader.ReadToEnd();
 
                if (statsCode != 200)
                {
                    throw new Exception("异常,响应码:" + statsCode.ToString());
                }
                res = JsonConvert.DeserializeObject<WebResponse>(responseContent);
            }
            catch (Exception ex)
            {
                res.Error(ex.Message);
            }
            finally
            {
                if (response != null)
                    response.Close();
                if (resultReader != null)
                    resultReader.Close();
            }
            return res;
        }
    }
}