C# - Lee 公共类库
Posted 贝尔塔猫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# - Lee 公共类库相关的知识,希望对你有一定的参考价值。
我的公共类库
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Text; using System.IO; using System.Net; using System.Web; using System.Web.UI; using CY.CommonLibrary.AspNet; using CY.CommonLibrary.Common; namespace Lee { public class Common { public static string Test_Swich = System.Configuration.ConfigurationManager.AppSettings["Test_Swich"]; //只要不为空,都为测试模式 /// <summary> /// 是否处于测试模式.只要不为空,都为测试模式 /// </summary> /// <returns>布尔值</returns> public static bool Is_测试模式() { if (Test_Swich != "") { return true; } return false; } /// <summary> /// send_发送POST请求 /// </summary> /// <param name="vServerPage"></param> /// <param name="vPostData"></param> /// <returns></returns> public static string send_发送POST请求(string vServerPage, string vPostData) #region { byte[] dataArray = Encoding.UTF8.GetBytes(vPostData); //创建请求 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(vServerPage); request.Method = "POST"; request.ContentLength = dataArray.Length; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 5000; //创建输入流 Stream dataStream = null; try { dataStream = request.GetRequestStream(); } catch (Exception) { return null;//连接服务器失败 } //发送请求 dataStream.Write(dataArray, 0, dataArray.Length); dataStream.Close(); //读取返回消息 string res = string.Empty; try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); res = reader.ReadToEnd(); reader.Close(); } catch (Exception ex) { return null;//连接服务器失败 } return res; } #endregion /// <summary> /// send_发送Get请求 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string send_发送GET请求(string url) #region { string str = ""; HttpWebRequest hwr = WebRequest.Create(url) as HttpWebRequest; hwr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; hwr.Method = "GET"; hwr.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:34.0) Gecko/20100101 Firefox/34.0"; hwr.ContentType = "application/x-www-form-urlencoded"; hwr.Headers["Accept-Language"] = "zh-cn"; hwr.Timeout = 3000; try { HttpWebResponse _rep = hwr.GetResponse() as HttpWebResponse; Stream _stream = _rep.GetResponseStream(); StreamReader read = new StreamReader(_stream, Encoding.UTF8); str = read.ReadToEnd(); _rep.Close(); } catch (Exception ex) { log.WriteLog("数据获取超时? :" + ex.Message + "请求地址为:" + url); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Write("<style type=\"text/css\">#face{margin:0px auto;background: #9ee675;/* for Webkit */background: -webkit-gradient(linear, left top, left bottom, from(#9ee675), to(#78cb4c));/* for Firefox */background: -moz-linear-gradient(top, #9ee675, #78cb4c);/* for IE */filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=‘#9ee675‘,endColorstr=‘#78cb4c‘); color:#fff;border:1px solid #fff;border-radius:200px;text-indent:31px;width:97px;height:97px;font-size:66px;} *{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: \"微软雅黑\"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 3.8em;text-align: center; font-size: 36px }</style><div style=\"padding: 24px 48px;\"> <h1 id=\"face\">:(</h1><p>数据超时..</p></div>"); HttpContext.Current.Response.End(); } return str; } #endregion /// <summary> /// 发送一张笑脸 /// </summary> public static void send_smiling_face(string str) #region { HttpContext.Current.Response.Write("<style type=\"text/css\">#face{margin:0px auto;background: #9ee675;/* for Webkit */background: -webkit-gradient(linear, left top, left bottom, from(#9ee675), to(#78cb4c));/* for Firefox */background: -moz-linear-gradient(top, #9ee675, #78cb4c);/* for IE */filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=‘#9ee675‘,endColorstr=‘#78cb4c‘); color:#fff;border:1px solid #fff;border-radius:200px;text-indent:31px;width:97px;height:97px;font-size:66px;} *{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: \"微软雅黑\"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 3.8em;text-align: center; font-size: 36px }</style><div style=\"padding: 24px 48px;\"> <h1 id=\"face\">:)</h1><p>" + str + "</p></div>"); } #endregion /// <summary> /// 发送一张哭脸 /// </summary> /// <param name="str"></param> public static void send_unhappy_face(string str) #region { HttpContext.Current.Response.Write("<style type=\"text/css\">#face{margin:0px auto;background: #9ee675;/* for Webkit */background: -webkit-gradient(linear, left top, left bottom, from(#9ee675), to(#78cb4c));/* for Firefox */background: -moz-linear-gradient(top, #9ee675, #78cb4c);/* for IE */filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=‘#9ee675‘,endColorstr=‘#78cb4c‘); color:#fff;border:1px solid #fff;border-radius:200px;text-indent:31px;width:97px;height:97px;font-size:66px;} *{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: \"微软雅黑\"; color: #333;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 3.8em;text-align: center; font-size: 36px }</style><div style=\"padding: 24px 48px;\"> <h1 id=\"face\">:(</h1><p>" + str + "</p></div>"); } #endregion /// <summary> /// 去除html标签 /// </summary> /// <param name="HTMLStr"></param> /// <returns></returns> public string ParseTags(string HTMLStr) #region { return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", ""); } #endregion /// <summary> /// 网页弹出框 /// </summary> /// <param name="obj"></param> public static void alert(object obj) { HttpContext.Current.Response.Write("<script language=javascript>alert(‘" + obj.ToString() + "‘)</script>"); } /// <summary> /// 网页弹出框 /// </summary> /// <param name="obj"></param> public static void alert_back(object obj) { HttpContext.Current.Response.Write("<script language=javascript>alert(‘" + obj.ToString() + "‘);history.back();</script>"); } /// <summary> /// 网页弹出框 /// </summary> /// <param name="obj"></param> public static void alert_href(object obj,string url) { HttpContext.Current.Response.Write("<script language=javascript>alert(‘" + obj.ToString() + "‘);window.location.href=‘" + url + "‘;</script>"); } /// <summary> /// 在控制台打印出数据 /// </summary> /// <param name="obj"></param> public static void log_Console(object obj) { HttpContext.Current.Response.Write("<script>console.log(\"" + obj + "\")</script>"); } /// <summary> /// 打印 /// </summary> /// <param name="obj"></param> public static void print(object obj) { HttpContext.Current.Response.Write(obj.ToString()); } /// <summary> /// 输出,不换行,使用完直接结束 /// </summary> /// <param name="obj"></param> public static void echo(object obj) { HttpContext.Current.Response.Write(obj.ToString()); } /// <summary> /// 打印,带换行 /// </summary> /// <param name="obj"></param> public static void println(object obj) { HttpContext.Current.Response.Write(obj.ToString() + "<br />"); } /// <summary> /// 结束网页输出 /// </summary> public static void End() { HttpContext.Current.Response.End(); } /// <summary> /// 清除网页输出 /// </summary> public static void Clear() { HttpContext.Current.Response.Clear(); } } }
以上是关于C# - Lee 公共类库的主要内容,如果未能解决你的问题,请参考以下文章
由于C#中ADO.NET对Oracle的命名空间引用时提示过时,为此想用Linq对数据库的连接等操作(见补充)