You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.9 KiB
C#

11 months ago
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebSqlHelper
{
public sealed class Common
{
// Methods
public static byte[] ConvertStringToByteArray(string s)
{
return new UnicodeEncoding().GetBytes(s);
}
public static string GetIP()
{
HttpRequest request = HttpContext.Current.Request;
string userHostAddress = string.Empty;
if ((request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null) || (request.ServerVariables["HTTP_X_FORWARDED_FOR"] == ""))
{
userHostAddress = request.ServerVariables["REMOTE_ADDR"];
}
else
{
int index;
if (request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(",") >= 0)
{
index = request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(",");
userHostAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Substring(0, index - 1);
}
else if (request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(";") >= 0)
{
index = request.ServerVariables["HTTP_X_FORWARDED_FOR"].IndexOf(";");
userHostAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"].Substring(0, index - 1);
}
else
{
userHostAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
}
if ((userHostAddress == null) || (userHostAddress == string.Empty))
{
userHostAddress = HttpContext.Current.Request.UserHostAddress;
}
if (userHostAddress == "127.0.0.1")
{
userHostAddress = GetLocalhostIPAddress();
}
return userHostAddress;
}
private static string GetLocalhostIPAddress()
{
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
string str2 = string.Empty;
for (int i = 0; i < addressList.Length; i++)
{
str2 = str2 + addressList[i].ToString();
}
return str2;
}
public static string GetNetIP()
{
try
{
Uri requestUri = new Uri("http://www.ikaka.com/ip/index.asp");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0;
request.CookieContainer = new CookieContainer();
request.GetRequestStream().Write(new byte[0], 0, 0);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string input = reader.ReadToEnd();
reader.Close();
request.Abort();
response.Close();
Match match = Regex.Match(input, @"(?<IP>\d+\.\d+\.\d+\.\d+)");
if (match.Success)
{
return match.Groups["IP"].Value;
}
return GetIP();
}
catch
{
return GetIP();
}
}
[DllImport("test.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true)]
public static extern string Out_Char(string str, string key);
public static string RndNum(int VcodeNum)
{
string[] strArray = "0,1,2,3,4,5,6,7,8,9".Split(new char[] { ',' });
string str2 = "";
Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 1; i < (VcodeNum + 1); i++)
{
int index = random.Next(strArray.Length);
str2 = str2 + strArray[index];
}
return str2;
}
public static void TrimChildText(Control parentControl)
{
foreach (Control c in parentControl.Controls)
{
if (c is TextBox)
{
TextBox txt = c as TextBox;
txt.Text = txt.Text.Trim();
}
}
}
public static bool isCntrNO(string no)
{
String regex = @"^[a-zA-Z]{4}\d{7}$";
if (Regex.IsMatch(no, regex))
{
return true;
}
else
{
return false;
}
}
}
}