/// <summary> /// 转换JObject /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jobj"></param> /// <returns></returns> public static T JObjectToObject <T>(JObject jobj) { try { return(jobj.ToObject <T>()); } catch (Exception ex) { Log4NetHelp.Error("将JObject转换为指定类型失败!" + ex.Message); return(default(T)); } }
/// <summary> /// http post发送 /// </summary> /// <param name="postUrl">post地址</param> /// <param name="paramData">post数据</param> /// <param name="contenttype">post格式</param> /// <param name="dataEncode">编码格式</param> /// <returns></returns> public static string PostWebRequest(string postUrl, string paramData, string contenttype, Encoding dataEncode) { string ret = string.Empty; HttpWebRequest webReq = null; HttpWebResponse response = null; StreamReader sr = null; Stream newStream = null; try { byte[] byteArray = dataEncode.GetBytes(paramData); //转化 webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = contenttype; webReq.ContentLength = byteArray.Length; webReq.KeepAlive = false; newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//写入数据流 newStream.Close(); response = (HttpWebResponse)webReq.GetResponse(); sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); // log4nethelp.Log4Logger.Info("POST数据:" + paramData + "至:" + postUrl + "成功! "); Log4NetHelp.Info("POST数据:" + paramData + "至:" + postUrl + "成功! "); } catch (Exception ex) { //log4nethelp.Log4Logger.Error("POST数据:" + paramData + "至:" + postUrl + "失败! " + ex.Message.ToString()); Log4NetHelp.Error("POST数据:" + paramData + "至:" + postUrl + "失败! " + ex.Message.ToString()); } finally { if (webReq != null) { webReq.Abort(); } if (sr != null) { sr.Close(); } if (response != null) { response.Close(); } if (newStream != null) { newStream.Close(); } } return(ret); }
/// <summary> ///反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="str"></param> /// <returns></returns> public static T StringToObject <T>(string str) { try { if (!string.IsNullOrEmpty(str)) { return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(str)); } else { return(default(T)); } } catch (Exception ex) { Log4NetHelp.Error("反序列化失败!数据体:" + str + " 错误:" + ex.Message); return(default(T)); } }