njcit/NJCIT/LoginServer.cs

132 lines
4.4 KiB
C#
Raw Permalink Normal View History

2019-09-14 12:12:26 +00:00
using System;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace NJCIT
{
class LoginServer
{
private static readonly string[] domainStr = new string[] { "studentphone", "student" };
private static readonly string initURL = "http://n.njcit.cn/index.php/index/init";
private static readonly string logoutURL = "http://n.njcit.cn/index.php/index/logout";
private static ArrayList result = null;
public static bool Online = false;
public static bool Logouted = false;
// 静态初始化
static LoginServer()
{
UpdateResult(HttpConect(initURL));
}
// 获取登录信息
public static ArrayList GetInformation()
{
UpdateResult(HttpConect(initURL));
return result;
}
// 执行登录
public static void DoLogin(string username, int domain, string password)
{
string[] textArray = new string[] { "http://n.njcit.cn/index.php/index/login?username=", username, "&domain=" + domainStr[domain] + "&password=", password, "&enablemacauth=1" };
UpdateResult(HttpConect(string.Concat(textArray)));
}
// 退出登录
public static void DoLogout()
{
UpdateResult(HttpConect(logoutURL));
Logouted = true;
}
// 处理登录返回结果
private static void UpdateResult(string str)
{
if (str != null)
{
string[] strArray = str.Replace("{", "").Replace("}", "").Replace("\"", "").Split(new char[] { ',' });
if (strArray != null)
{
ArrayList list = new ArrayList();
if (strArray.Length == 3)
{
list.Add("认证失败, 请检查密码及账户状态!");
Online = false;
}
else if (strArray.Length == 1)
{
list.Add("认证失败,账户已锁定!");
Online = false;
}
else if (strArray.Length == 4)
{
list.Add("认证失败,并发登录超过最大限制!");
Online = false;
}
else
{
list.Add("用户已登录");
if (strArray.Length == 11)
{
list.Add(ParseData(strArray[2]));
list.Add(ParseData(strArray[3]));
list.Add(ParseData(strArray[4]));
list.Add(ParseData(strArray[6]));
list.Add(ParseData(strArray[8]));
list.Add(ParseData(strArray[9]));
}
else
{
list.Add(ParseData(strArray[2]));
list.Add(ParseData(strArray[3]));
list.Add(ParseData(strArray[4]));
list.Add(ParseData(strArray[6]));
list.Add(ParseData(strArray[9]));
list.Add(ParseData(strArray[10]));
}
Online = true;
}
result = list;
}
}
}
// Http连接
private static string HttpConect(string URL)
{
Ping ping = new Ping();
PingReply reply = ping.Send("n.njcit.cn", 1000);
if (reply.Status != IPStatus.Success)
{
return null;
}
try
{
WebClient client = new WebClient
{
Credentials = CredentialCache.DefaultCredentials
};
byte[] bytes = client.DownloadData(URL);
return Encoding.UTF8.GetString(bytes);
}
catch (Exception)
{
return null;
}
}
// 解析数据
private static string ParseData(string str)
{
char[] separator = new char[] { ':' };
return str.Split(separator)[1];
}
}
}