diff --git a/App.code/Login/HttpMethods.cs b/App.code/Login/HttpMethods.cs new file mode 100644 index 0000000..85ea5fc --- /dev/null +++ b/App.code/Login/HttpMethods.cs @@ -0,0 +1,246 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using System.Net; + + +namespace basic +{ + public class HttpMethods + { + + #region POST + /// + /// HTTP POST方式请求数据 + /// + /// URL. + /// POST的数据 + /// + public static string HttpPost(string url, string param) + { + HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); + request.Method = "POST"; + request.ContentType = "application/x-www-form-urlencoded"; + request.Accept = "*/*"; + request.Timeout = 15000; + request.AllowAutoRedirect = false; + + StreamWriter requestStream = null; + WebResponse response = null; + string responseStr = null; + + try + { + requestStream = new StreamWriter(request.GetRequestStream()); + requestStream.Write(param); + requestStream.Close(); + + response = request.GetResponse(); + if (response != null) + { + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); + responseStr = reader.ReadToEnd(); + reader.Close(); + } + } + catch (Exception) + { + throw; + } + finally + { + request = null; + requestStream = null; + response = null; + } + + return responseStr; + } + #endregion + + #region Get + /// + /// HTTP GET方式请求数据. + /// + /// URL. + /// + public static string HttpGet(string url) + { + HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); + request.Method = "GET"; + //request.ContentType = "application/x-www-form-urlencoded"; + request.Accept = "*/*"; + request.Timeout = 15000; + request.AllowAutoRedirect = false; + + WebResponse response = null; + string responseStr = null; + + try + { + response = request.GetResponse(); + + if (response != null) + { + StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); + responseStr = reader.ReadToEnd(); + reader.Close(); + } + } + catch (Exception) + { + throw; + } + finally + { + request = null; + response = null; + } + + return responseStr; + } + #endregion + + #region Post With Pic + private string HttpPost(string url, IDictionary param, string filePath) + { + string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); + byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); + + HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); + wr.ContentType = "multipart/form-data; boundary=" + boundary; + wr.Method = "POST"; + wr.KeepAlive = true; + wr.Credentials = System.Net.CredentialCache.DefaultCredentials; + + Stream rs = wr.GetRequestStream(); + string responseStr = null; + + string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; + foreach (string key in param.Keys) + { + rs.Write(boundarybytes, 0, boundarybytes.Length); + string formitem = string.Format(formdataTemplate, key, param[key]); + byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); + rs.Write(formitembytes, 0, formitembytes.Length); + } + rs.Write(boundarybytes, 0, boundarybytes.Length); + + string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; + string header = string.Format(headerTemplate, "pic", filePath, "text/plain"); + byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); + rs.Write(headerbytes, 0, headerbytes.Length); + + FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); + byte[] buffer = new byte[4096]; + int bytesRead = 0; + while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) + { + rs.Write(buffer, 0, bytesRead); + } + fileStream.Close(); + + byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); + rs.Write(trailer, 0, trailer.Length); + rs.Close(); + + WebResponse wresp = null; + try + { + wresp = wr.GetResponse(); + Stream stream2 = wresp.GetResponseStream(); + StreamReader reader2 = new StreamReader(stream2); + responseStr = reader2.ReadToEnd(); + //logger.Debug(string.Format("File uploaded, server response is: {0}", responseStr)); + } + catch (Exception ex) + { + //logger.Error("Error uploading file", ex); + if (wresp != null) + { + wresp.Close(); + wresp = null; + } + } + finally + { + wr = null; + } + return responseStr; + } + #endregion + + #region Post With Pic + /// + /// HTTP POST方式请求数据(带图片) + /// + /// URL + /// POST的数据 + /// 图片 + /// + public static string HttpPost(string url, IDictionary param, byte[] fileByte) + { + string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); + byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); + + HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); + wr.ContentType = "multipart/form-data; boundary=" + boundary; + wr.Method = "POST"; + wr.KeepAlive = true; + wr.Credentials = System.Net.CredentialCache.DefaultCredentials; + + Stream rs = wr.GetRequestStream(); + string responseStr = null; + + string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; + foreach (string key in param.Keys) + { + rs.Write(boundarybytes, 0, boundarybytes.Length); + string formitem = string.Format(formdataTemplate, key, param[key]); + byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); + rs.Write(formitembytes, 0, formitembytes.Length); + } + rs.Write(boundarybytes, 0, boundarybytes.Length); + + string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; + string header = string.Format(headerTemplate, "pic", fileByte, "text/plain");//image/jpeg + byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); + rs.Write(headerbytes, 0, headerbytes.Length); + + rs.Write(fileByte, 0, fileByte.Length); + + byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); + rs.Write(trailer, 0, trailer.Length); + rs.Close(); + + WebResponse wresp = null; + try + { + wresp = wr.GetResponse(); + Stream stream2 = wresp.GetResponseStream(); + StreamReader reader2 = new StreamReader(stream2); + responseStr = reader2.ReadToEnd(); + // logger.Error(string.Format("File uploaded, server response is: {0}", responseStr)); + } + catch (Exception ex) + { + //logger.Error("Error uploading file", ex); + if (wresp != null) + { + wresp.Close(); + wresp = null; + } + } + finally + { + wr = null; + } + return responseStr; + } + #endregion + } + + + +} diff --git a/App.code/Login/QQConfig.cs b/App.code/Login/QQConfig.cs new file mode 100644 index 0000000..2f67663 --- /dev/null +++ b/App.code/Login/QQConfig.cs @@ -0,0 +1,44 @@ +using System.Web; +using System.Text; +using System.IO; +using System.Net; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SqlClient; + +namespace basic +{ + /// + /// 类名:QQConfig + /// 功能:基础配置类 + /// + public class QQConfig + { + private static string client_id = "";//qzone_AppID + private static string client_secret = "";//qzone_AppKey + private static string redirect_uri = "";//qzone_Redirect_uri + static QQConfig() + { + client_id = "101168247"; + client_secret = "b06ef183d683728572d456ea66ebecc3"; + string strUrl = HttpContext.Current.Request.Url.Host; + redirect_uri = "http://" + strUrl + "/QQlogin/callback.aspx"; + } + public static string Client_id + { + get { return QQConfig.client_id; } + set { QQConfig.client_id = value; } + } + public static string Client_secret + { + get { return QQConfig.client_secret; } + set { QQConfig.client_secret = value; } + } + public static string Redirect_uri + { + get { return QQConfig.redirect_uri; } + set { QQConfig.redirect_uri = value; } + } + } +} \ No newline at end of file diff --git a/App.code/Login/Utils.cs b/App.code/Login/Utils.cs new file mode 100644 index 0000000..efe0dd0 --- /dev/null +++ b/App.code/Login/Utils.cs @@ -0,0 +1,694 @@ +using System; +using System.Text; +using System.IO; +using System.Data; +using System.Configuration; +using System.Web; +using System.Web.Security; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.Web.UI.WebControls.WebParts; +using System.Web.UI.HtmlControls; +using System.Text.RegularExpressions; + +namespace basic +{ + public class Utils + { + /// + /// ȡAppSetingַ + /// + /// Ҫȡַ + /// + public static string GetAppSeting(string key) + { + + if (key == null || key == "") + return ""; + string outstr = ""; + outstr = System.Configuration.ConfigurationManager.AppSettings[key]; + if (outstr == null || outstr == "") + return ""; + + return outstr; + } + + /// + /// תUnicodeToGB磬\u548cĺ + /// + /// + /// + private string UnicodeToGB(string content) + { + Regex objRegex = new Regex("&#(?[\\d]{5});", RegexOptions.IgnoreCase); + Match objMatch = objRegex.Match(content); + System.Text.StringBuilder sb = new System.Text.StringBuilder(content); + while (objMatch.Success) + { + string code = Convert.ToString(Convert.ToInt32(objMatch.Result("${UnicodeCode}")), 16); + byte[] array = new byte[2]; + array[0] = (byte)Convert.ToInt32(code.Substring(2), 16); + array[1] = (byte)Convert.ToInt32(code.Substring(0, 2), 16); + + sb.Replace(objMatch.Value, System.Text.Encoding.Unicode.GetString(array)); + + objMatch = objMatch.NextMatch(); + } + return sb.ToString(); + } + + + /// + /// ȡַλ,λ1 + /// + public static string getstr(string str1, int leng) + { + string tmp = str1; + if (leng > 0) + { + if (tmp.Length > leng) + { + tmp.Remove(leng); + tmp = tmp + "..."; + } + } + return tmp; + } + + /// + /// ȡַ + /// + /// Ҫȡַ + /// ȡַij + /// + public static string GetSubString(string Str, int Num) + { + if (Str == null || Str == "") + return ""; + string outstr = ""; + int n = 0; + foreach (char ch in Str) + { + n += System.Text.Encoding.Default.GetByteCount(ch.ToString()); + if (n > Num) + break; + else + outstr += ch; + } + return outstr; + } + + /// + /// ȡַ + /// + /// Ҫȡַ + /// ȡַij + /// ȡַʡԲֵַ + /// + public static string GetSubString(string Str, int Num, string LastStr) + { + return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str; + } + + /// + /// MD5ַ + /// + /// 16λ32λΪtrueΪ16λ + /// ַ + /// + public static string MD5(string Input, bool Half) + { + string output = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Input, "MD5").ToLower(); + if (Half)//16λMD5ܣȡ32λܵ9~25ַ + output = output.Substring(8, 16); + return output; + } + + public static string MD5(string Input) + { + return MD5(Input, true); + } + + + /// + /// Sql + /// + /// + /// + public static string FilterSql(string sql) + { + sql = sql.Replace("'", "''"); + return sql; + } + + + /// + /// ûǷ + /// + /// ûύ + /// ǷSQLעʽ + private bool ProcessSqlStr(string Str) + { + bool ReturnValue = true; + try + { + if (Str.Trim() != "") + { + string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare"; + + string[] anySqlStr = SqlStr.Split('|'); + foreach (string ss in anySqlStr) + { + if (Str.ToLower().IndexOf(ss) >= 0) + { + ReturnValue = false; + break; + } + } + } + } + catch + { + ReturnValue = false; + } + return ReturnValue; + } + + + /// + /// ִһJS + /// + /// Ҫִе + public static void ExecuteJs(string sentence) + { + HttpContext.Current.Response.Write(""); + } + + + + /// + /// ж϶ǷΪInt32͵ + /// + /// + /// + public static bool IsNumeric(object expression) + { + if (expression != null) + { + return IsNumeric(expression.ToString()); + } + return false; + + } + + + + + /// + /// ж϶ǷΪInt32͵ + /// + /// + /// + public static bool IsNumeric(string expression) + { + if (expression != null) + { + string str = expression; + if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) + { + if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) + { + return true; + } + } + } + return false; + + } + /// + /// ǷΪDouble + /// + /// + /// + public static bool IsDouble(object expression) + { + if (expression != null) + { + return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); + } + return false; + } + + /// + /// stringתΪbool + /// + /// Ҫתַ + /// ȱʡֵ + /// תboolͽ + public static bool StrToBool(object expression, bool defValue) + { + if (expression != null) + { + return StrToBool(expression, defValue); + } + return defValue; + } + + /// + /// stringתΪbool + /// + /// Ҫתַ + /// ȱʡֵ + /// תboolͽ + public static bool StrToBool(string expression, bool defValue) + { + if (expression != null) + { + if (string.Compare(expression, "true", true) == 0) + { + return true; + } + else if (string.Compare(expression, "false", true) == 0) + { + return false; + } + } + return defValue; + } + + /// + /// תΪInt32 + /// + /// Ҫתַ + /// ȱʡֵ + /// תintͽ + public static int StrToInt(object expression, int defValue) + { + if (expression != null) + { + return StrToInt(expression.ToString(), defValue); + } + return defValue; + } + + /// + /// תΪInt32 + /// + /// Ҫתַ + /// ȱʡֵ + /// תintͽ + public static int StrToInt(string str, int defValue) + { + if (str == null) + return defValue; + if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$")) + { + if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) + { + return Convert.ToInt32(str); + } + } + return defValue; + } + + /// + /// stringתΪfloat + /// + /// Ҫתַ + /// ȱʡֵ + /// תintͽ + public static float StrToFloat(object strValue, float defValue) + { + if ((strValue == null)) + { + return defValue; + } + + return StrToFloat(strValue.ToString(), defValue); + } + + /// + /// stringתΪfloat + /// + /// Ҫתַ + /// ȱʡֵ + /// תintͽ + public static float StrToFloat(string strValue, float defValue) + { + if ((strValue == null) || (strValue.Length > 10)) + { + return defValue; + } + + float intValue = defValue; + if (strValue != null) + { + bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); + if (IsFloat) + { + intValue = Convert.ToSingle(strValue); + } + } + return intValue; + } + + + /// + /// жϸַ(strNumber)еDzǶΪֵ + /// + /// Ҫȷϵַ + /// 򷵼true 򷵻 false + public static bool IsNumericArray(string[] strNumber) + { + if (strNumber == null) + { + return false; + } + if (strNumber.Length < 1) + { + return false; + } + foreach (string id in strNumber) + { + if (!IsNumeric(id)) + { + return false; + } + } + return true; + + } + + /// + /// дcookieֵ + /// + /// + /// ֵ + public static void WriteCookie(string strName, string strValue) + { + HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; + if (cookie == null) + { + cookie = new HttpCookie(strName); + } + cookie.Value = strValue; + HttpContext.Current.Response.AppendCookie(cookie); + + } + + /// + /// дcookieֵ + /// + /// + /// ֵ + public static void WriteCookie(string strName, string key, string strValue) + { + HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; + if (cookie == null) + { + cookie = new HttpCookie(strName); + } + cookie[key] = strValue; + HttpContext.Current.Response.AppendCookie(cookie); + + } + /// + /// дcookieֵ + /// + /// + /// ֵ + /// ʱ() + public static void WriteCookie(string strName, string strValue, int expires) + { + HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; + if (cookie == null) + { + cookie = new HttpCookie(strName); + } + cookie.Value = strValue; + cookie.Expires = DateTime.Now.AddMinutes(expires); + HttpContext.Current.Response.AppendCookie(cookie); + + } + + /// + /// cookieֵ + /// + /// + /// cookieֵ + public static string GetCookie(string strName) + { + if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) + { + return HttpContext.Current.Request.Cookies[strName].Value.ToString(); + } + + return ""; + } + + /// + /// cookieֵ + /// + /// + /// cookieֵ + public static string GetCookie(string strName, string key) + { + if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) + { + return HttpContext.Current.Request.Cookies[strName][key].ToString(); + } + + return ""; + } + + /// + /// õǰҳͻ˵IP + /// + /// ǰҳͻ˵IP + public static string GetIP() + { + string result = String.Empty; + + result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; + if (string.IsNullOrEmpty(result)) + { + result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; + } + + if (string.IsNullOrEmpty(result)) + { + result = HttpContext.Current.Request.UserHostAddress; + } + + if (result == "127.0.0.1") + { + result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"]; + if (result == "") + { result = "127.0.0.1"; } + } + + return result; + + } + + /// + /// ǷΪip + /// + /// + /// + public static bool IsIP(string ip) + { + return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); + + } + + + /// + /// жǷʱʽ + /// + /// + /// + public static bool IsTime(string str) + { + bool bol = false; + DateTime Dt = new DateTime(); + if (DateTime.TryParse(str, out Dt)) + { + bol = true; + } + else + { + bol = false; + } + return bol; + } + + + + /// + /// жϵǰҳǷյPost + /// + /// ǷյPost + public static bool IsPost() + { + return HttpContext.Current.Request.HttpMethod.Equals("POST"); + } + /// + /// жϵǰҳǷյGet + /// + /// ǷյGet + public static bool IsGet() + { + return HttpContext.Current.Request.HttpMethod.Equals("GET"); + } + + /// + /// ֵָ + /// + /// + /// ֵ + public static string GetFileFullPath(string strName) + { + if (HttpContext.Current.Server.MapPath(strName) == null) + { + return ""; + } + return HttpContext.Current.Server.MapPath(strName); + } + + /// + /// 滻ַеĿո + /// + /// + /// + public static string ReplaceSpace(string strName) + { + + return strName.Replace(" ", "-").Replace("&", "-"); + + } + + + /// + /// ֵָ + /// + /// + /// ֵ + public static string GetFormString(string strName) + { + if (HttpContext.Current.Request.Form[strName] == null) + { + return ""; + } + return HttpContext.Current.Request.Form[strName]; + } + + /// + /// ָUrlֵ + /// + /// Url + /// Urlֵ + public static string GetQueryString(string strName) + { + if (HttpContext.Current.Request.QueryString[strName] == null) + { + return ""; + } + return HttpContext.Current.Request.QueryString[strName]; + } + + /// + /// õǰUrlַ + /// + /// ǰUrlַ + public static string GetUrl() + { + return HttpContext.Current.Request.Url.ToString(); + } + + /// + /// һҳĵַ + /// + /// һҳĵַ + public static string GetUrlReferrer() + { + string retVal = null; + + try + { + retVal = HttpContext.Current.Request.UrlReferrer.ToString(); + } + catch { } + + if (retVal == null) + return ""; + + return retVal; + + } + /// + /// htmlǩ + /// + /// + /// + public static string FilterHtmlStr(string html) + { + System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"

", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"

", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); + html = regex1.Replace(html, ""); // + html = regex2.Replace(html, ""); //href=javascript: () + html = regex3.Replace(html, " _disibledevent="); //ؼon...¼ + html = regex4.Replace(html, ""); //iframe + html = regex5.Replace(html, ""); //frameset + html = regex6.Replace(html, ""); //frameset + html = regex7.Replace(html, ""); //frameset + html = regex8.Replace(html, ""); //frameset + html = regex9.Replace(html, ""); + //html = html.Replace(" ", ""); + html = html.Replace("", ""); + html = html.Replace("", ""); + return html; + } + + + ///

+ /// URL· + /// + /// + public static string UrlEncode(string strHtml) + { + return HttpUtility.UrlEncode(strHtml, Encoding.Default); + + } + + /// + /// URL· + /// + /// + public static string UrlDecode(string strHtml) + { + return HttpUtility.UrlDecode(strHtml, Encoding.Default); + } + + ///// + ///// URL· + ///// + ///// + //public static string UrlPathEncode(string strHtml) + //{ + // return HttpUtility.UrlPathEncode(strHtml); + //} + + + + } +}