HttpWebRequest 模拟网站登录获取数据

  此文档仅仅是一个BaseCode,已做后续查阅

  项目使用IBM Platform Symphony分布式平台,所有业务处理都在这个分布式平台上计算,需求是获取这些计算机机群的运行状态,和每一个服务的的运行状态。

  这个数据来源在一个PMC的 IBM Platform Symphony本地平台上。

  通过HttpWatch工具得到登录和获取数据的Uri:

    1、http://dane55-pc:8080/platform/dealUserLogin.do

    2、http://dane55-pc:8080/symgui/login.jsp?&login=y&loginPassword=Admin&loginUserName=Admin&redirectURL=&command=login

    3、http://dane55-pc:8080/symgui/j_spring_security_check

    4、http://dane55-pc:8080/symgui/sym/resources/clusterhealth.action?rnd=1382084686523

    5、http://dane55-pc:8080/symgui/sym/application/doApplicationProperties.action?appName=symping6.1&rnd=1382076186197

  下面是处理类:

/// <summary>
/// IBM Platform Symphony Standard Edition 处理类
/// </summary>
public class IBMPlatformSymphonyStandardEdition
{
private bool _isLogin;
/// 设置和获取登录状态
public bool IsLogin
{
get { return _isLogin; }
set { _isLogin = value; }
}
/// 登录失败后重定向的Uri
private string _baseUri = string.Empty; private CookieContainer _container = new CookieContainer(); private Encoding _encoding = new UTF8Encoding(); private HttpWebRequest _webRequest; private HttpWebResponse _webResponse; private string _postData = string.Empty; private byte[] _data = new byte[]; private string _hostName = string.Empty, _hostPort = string.Empty;
/// 构造函数
public IBMPlatformSymphonyStandardEdition()
{
this._hostName = "dane55-pc";
this._hostPort = "";
this._baseUri = "http://dane55-pc:8080/symgui/framework/login/toLogin.action";
this._isLogin = false;
}
/// 构造函数 指定主机名称和端口
public IBMPlatformSymphonyStandardEdition(string hostName, string hostPort)
{
this._hostName = hostName;
this._hostPort = hostPort;
this._baseUri = "http://" + hostName + ":" + hostPort + "/symgui/framework/login/toLogin.action";
this._isLogin = false;
}
/// 登录
public bool LoginSymphony(string userName, string userPass)
{
try
{
#region http://dane55-pc:8080/platform/dealUserLogin.do _postData = "command=login&loginUserName=" + userName + "&redirectURL=&login=y&loginPassword=" + userPass;
_data = _encoding.GetBytes(_postData);
_webRequest = (HttpWebRequest)WebRequest.Create("http://" + _hostName + ":" + _hostPort + "/platform/dealUserLogin.do");
_webRequest.Method = "Post";
_webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
_webRequest.ContentLength = _data.Length;
_webRequest.KeepAlive = true;
_webRequest.CookieContainer = _container;
_webRequest.GetRequestStream().Write(_data, , _data.Length);
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
#endregion #region http://dane55-pc:8080/symgui/login.jsp?&login=y&loginPassword=Admin&loginUserName=Admin&redirectURL=&command=login string uri = "http://" + _hostName + ":" + _hostPort + "/symgui/login.jsp?&login=y&loginPassword=" + userPass + "&loginUserName=" + userName + "&redirectURL=&command=login";
_webRequest = (HttpWebRequest)WebRequest.Create(uri);
_webRequest.Method = "GET";
_webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
_webRequest.KeepAlive = true;
_webRequest.CookieContainer = _container;
_webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
StreamReader readers = new StreamReader(_webResponse.GetResponseStream(), Encoding.UTF8);
string contents = readers.ReadToEnd();
readers.Close();
readers.Dispose();
string j_username = string.Empty;
string j_password = string.Empty;
Regex _regex = new Regex(@"(?is)<input[^/>]*id=(?<id>\s*)[^/>]*value(?<v>\s*)[^/>]*/>");
if (_regex.IsMatch(contents))
{
MatchCollection collection = _regex.Matches(contents);
if (collection[].ToString().IndexOf("name=\"j_username\"") > -)
{
_regex = new Regex("value=\"(.+?)\"");
if (_regex.IsMatch(collection[].ToString()))
{
Match aas = _regex.Match(collection[].ToString());
j_username = aas.ToString().Replace("value=\"", "").Replace("\"", "");
}
}
if (collection[].ToString().IndexOf("name=\"j_username\"") > -)
{
_regex = new Regex("value=\"(.+?)\"");
if (_regex.IsMatch(collection[].ToString()))
{
Match aas = _regex.Match(collection[].ToString());
j_password = aas.ToString().Replace("value=\"", "").Replace("\"", "");
}
} }
#endregion #region http://dane55-pc:8080/symgui/j_spring_security_check string uri1 = "http://" + this._hostName + ":" + this._hostPort + "/symgui/j_spring_security_check";
_webRequest = (HttpWebRequest)WebRequest.Create(uri1);
_postData = "j_password=" + j_password + "&j_username=" + j_username;
_data = _encoding.GetBytes(_postData);
_webRequest.Method = "POST";
_webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
_webRequest.KeepAlive = true;
_webRequest.CookieContainer = _container;
_webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
_webRequest.GetRequestStream().Write(_data, , _data.Length);
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
if (_webRequest.Address.AbsoluteUri == _baseUri) return false;
#endregion
}
catch (Exception)
{
throw new Exception("登录Symphony平台发生异常");
}
return true;
}
/// 获取平台机群数据以及运行状态
public Masters GetHostData()
{
if (!this._isLogin) return null;
string url = "http://" + _hostName + ":" + _hostPort + "/symgui/sym/resources/clusterhealth.action?rnd=" + this.GetRnd();
return GetHostData(url);
}
/// 获取平台机群数据以及运行状态
public Masters GetHostData(string url)
{
try
{
Masters master = null;
if (!this._isLogin) return null;
_webRequest = (HttpWebRequest)WebRequest.Create(url);
_webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
_webRequest.Method = "GET";
_webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
_webRequest.ContentLength = ;
_webRequest.KeepAlive = true;
_webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
_webRequest.CookieContainer = _container;
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
StreamReader reader = new StreamReader(_webResponse.GetResponseStream(), Encoding.UTF8);
string szJson = reader.ReadToEnd();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
Masters obj = Activator.CreateInstance<Masters>();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
master = (Masters)serializer.ReadObject(ms);
}
return master;
}
catch (Exception)
{
throw new Exception("获取Symphony平台机群信息异常");
}
}
/// 获取指定服务名称运行状态信息
public ApplicationProperties GetApplicationPropertiesData(string appName)
{
try
{
string url = "http://" + _hostName + ":" + _hostPort + "/symgui/sym/application/doApplicationProperties.action?appName=" + appName + "&rnd=" + GetRnd();
ApplicationProperties application = null;
if (!this._isLogin) return null;
_webRequest = (HttpWebRequest)WebRequest.Create(url);
_webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
_webRequest.Method = "GET";
_webRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
_webRequest.ContentLength = ;
_webRequest.KeepAlive = true;
_webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
_webRequest.CookieContainer = _container;
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
StreamReader reader = new StreamReader(_webResponse.GetResponseStream(), Encoding.UTF8);
string szJson = reader.ReadToEnd();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
ApplicationProperties obj = Activator.CreateInstance<ApplicationProperties>();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
application = (ApplicationProperties)serializer.ReadObject(ms);
}
return application;
}
catch (Exception)
{
throw new Exception("获取Symphony平台ApplicationProperties信息异常");
}
}
/// 防止获取浏览器缓存数据
private string GetRnd()
{
DateTime oldTime = new DateTime(, , );
TimeSpan span = DateTime.Now.Subtract(oldTime);
return span.TotalMilliseconds.ToString("F0");
}
}
  public class ApplicationProperties
{
public string customerStore;
public string dir;
public int pageSize;
public List<Records> records;
public ReturnMsgs returnMsgs;
public string sort;
public int startIndex;
public int totalRecords;
} public class Records
{
public string defaultValue;
public string key;
public string value;
}
/// 查询索引器
public class RecordsIndexer
{
IDictionary<string, string> _dictionary;
public RecordsIndexer(List<Records> recordsList)
{
_dictionary = new Dictionary<string, string>();
foreach (Records records in recordsList)
{
_dictionary.Add(records.key, records.value);
}
}
public string this[string key]
{
get
{
if (key == "")
{
return null;
}
else
{
return _dictionary[key];
}
}
set
{
if (key != "")
{
_dictionary[key] = value;
}
}
}
} public class ReturnMsgs
{
public List<String> failure;
public string success;
}
    [Serializable]
public class Master
{
public string hostName;
public string mem;
public string io;
public string pg;
public string ncpus;
public string hostStatus;
public string type;
public string swp;
public string ut;
} [Serializable]
public class ClusterInfo
{
public int unavail_hosts;
public string clusterName;
public int ut_hosts0;
public int ut_hosts1;
public int ut_hosts2;
public int ok_hosts;
public int ut_hosts3;
public int ut_hosts4;
public int ut_hosts5;
public int ut_hosts6;
public int ut_hosts7;
public int ut_hosts8;
public int all_hosts;
public int scavenging_closed_hosts;
public int ut_hosts9;
public int closed_hosts;
public int scavenging_ok_hosts;
} [Serializable]
public class AllHostGroups
{
public string InternalResourceGroup;
public string ComputeHosts;
public string ManagementHosts;
} [Serializable]
public class Masters
{
public List<Master> MasterList = new List<Master>(); public List<String> candidateHosts = new List<string>(); public ClusterInfo ClusterInfo = new ClusterInfo(); public Master sdHost = new Master(); public List<String> allHosts = new List<string>(); public Master MasterHost = new Master(); public AllHostGroups allhostgroups = new AllHostGroups();
}

  运行测试:  

       static IBMPlatformSymphonyStandardEdition edtion = new IBMPlatformSymphonyStandardEdition();
static void Main(string[] args)
{
bool _isBool = edtion.LoginSymphony("Admin", "Admin");
if (!_isBool)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("登录失败,正在重新登录");
Console.BackgroundColor = ConsoleColor.Black;
}
else
{
edtion.IsLogin = _isBool;
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine("登录成功");
Console.BackgroundColor = ConsoleColor.Black;
Masters _masters = edtion.GetHostData();
ApplicationProperties applications = edtion.GetApplicationPropertiesData("symping6.1");
foreach (var item in _masters.MasterList)
{
Console.WriteLine(item.hostName);
}
foreach (Records item in applications.records)
{
RecordsIndexer indexer = new RecordsIndexer(applications.records);
Console.BackgroundColor = ConsoleColor.Green;
Console.WriteLine(indexer["appName"]);
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine(item.value);
} }
}

  

上一篇:eclipse中实现文本换行


下一篇:【Caffe 测试】Training LeNet on MNIST with Caffe