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(); /// /// 带参数的Post请求 /// /// /// /// public static string PostByPara(string Url, Dictionary 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; } /// /// post请求 /// /// /// 参数 /// 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(responseContent); } catch (Exception ex) { res.Error(ex.Message); } finally { if (response != null) response.Close(); if (resultReader != null) resultReader.Close(); } return res; } } }