HttpHelper

using Newtonsoft.Json;
using ShareSharp.Log;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CSCloudMes
{
    public class HttpHelper
    {
        private HttpHelper() { }
        public static HttpHelper Instance { get; } = new HttpHelper();

        private string GetHttpGetURL(string url, Dictionary<string, object> parameters)
        {
            List<string> list = new List<string>();

            foreach (var key in parameters.Keys)
            {
                list.Add($"{key}={parameters[key]}");
            }

            return url + (list.Count == 0 ? "" : "?") + string.Join("&", list);
        }

        public string HttpGet(string Url, Dictionary<string, object> paramters)
        {
            string urlRequestString = GetHttpGetURL(Url, paramters);
            Logger.WriteLog(urlRequestString, "CSCloudMes");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlRequestString);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            Logger.WriteLog(retString, "CSCloudMes");
            return retString;
        }


        public CSCloudMesResult UploadAlarmInfo(string url, string devId, string devName, string devType, string devLocation, string eventMessage, string eventLogMsg, string eventSource, string eventType, string eventSeverity, string eventDatetime, string eventParameter)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic[nameof(devId)] = devId;
            dic[nameof(devName)] = devName;
            dic[nameof(devType)] = devType;
            dic[nameof(devLocation)] = devLocation;
            dic[nameof(eventMessage)] = eventMessage;
            dic[nameof(eventLogMsg)] = eventLogMsg;
            dic[nameof(eventSource)] = eventSource;
            dic[nameof(eventType)] = eventType;
            dic[nameof(eventSeverity)] = eventSeverity;
            dic[nameof(eventDatetime)] = eventDatetime;
            dic[nameof(eventParameter)] = eventParameter;

            string json = HttpGet(url, dic);

            return JsonConvert.DeserializeObject<CSCloudMesResult>(json);
        }

        public CSCloudMesResult ValidBatchNo(string url, string batchno, string devId)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic[nameof(batchno)] = batchno;
            dic[nameof(devId)] = devId;
            string json = HttpGet(url, dic);
            return JsonConvert.DeserializeObject<CSCloudMesResult>(json);
        }

        public class CSCloudMesResult
        {
            public bool status;
            public int errcode;
            public List<ArrayElem> result;
            public string message;
        }

        public class ArrayElem
        {
            public string paraname;
            public string paravalue;
        }
    }
}

上一篇:Python爬取豆瓣排名报错参考——关键词:换行符|中文乱码|返回空值|正则问题


下一篇:Python编程题36--三个数的最大乘积