目的,把远程服务器传回的Html,解析到类里面,为GridView等提供数据源
1、向远程服务器邮政数据
public int PostData(字符串 url, 字符串数据, 输出字符串信息)
{
信息 = "";
CookieContainer cc = new CookieContainer();
HttpWebRequest 请求 = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = cc;
请求方法 = "POST";
request.ContentType = "application/x-www-form-urlencoded";
流 requestStream = request.GetRequestStream();
byte[] byteArray = Encoding.UTF8.GetBytes(data);
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
HttpWebResponse 响应 = request.GetResponse() as HttpWebResponse;
Uri 响应Uri = 响应.ResponseUri;
流 receiveStream = response.GetResponseStream();
编码encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, 编码);
字符串结果 = readStream.ReadToEnd();
信息=结果;
返回0;
}2、解析返回的html,有简洁
公共 ClassInfo[] GetClass(字符串 html)
{
ArrayList ar = new ArrayList();
ArrayList arr = new ArrayList();
字符串表=“”;
正则表达式 regtable = new Regex(@"(?<=<table.*>).*?(?=</table>)", RegexOptions.Singleline);
匹配ma = regtable.Match(html);
while (ma.成功)
{
if (ma.Value.Trim() != "")
{
arr.Add(HttpUtility.HtmlDecode(ma.Value));
}
ma = ma.NextMatch();
}
for (int i = 0; i < arr.Count; i++)
{
表 = arr[i].ToString() + 表;
}
正则表达式 reg = new Regex(@"(?<=<.*?>).*?(?=<.*?>)", RegexOptions.Singleline);
匹配 m = reg.Match(table);
while (m.成功)
{
if (m.Value.Trim() != "")
{
ar.Add(HttpUtility.HtmlDecode(m.Value));
}
m = m.NextMatch();
}
ClassInfo[] ci = new ClassInfo[classno];
for (int i = 0; i < 类号; i++)
{
ci[i] = new ClassInfo();
ci[i].RegisterDate = (ar[i * 8 + 0]).ToString();
ci[i].LoginDate = (ar[i * 8 + 1]).ToString();
ci[i].LogoutDate = (ar[i * 8 + 2]).ToString();
ci[i].UseMin = ar[i * 8 + 3].ToString();
ci[i].ClassName = ar[i * 8 + 5].ToString();
ci[i].ClassType = ar[i * 8 + 6].ToString();
ci[i].Percent = ar[i * 8 + 7].ToString();
}
返回ci;
}
3、定义ClassInfo类(课程类),用CodeSmith生成
公共类类信息
{
成员变量#region 成员变量
受保护的字符串_loginDate;
受保护的字符串_logoutDate;
受保护的字符串_registerDate;
受保护的字符串_useMin;
受保护的字符串_className;
受保护的字符串_classType;
受保护的字符串_percent;
//受保护的字符串_nouse;
#endregion
构造函数#region 构造函数
public ClassInfo() { }
public ClassInfo(string loginDate, string logoutDate, string registerDate, string useMin, string className, string classType, string Percent)
{
this._loginDate = 登录日期;
this._logoutDate = 注销日期;
this._registerDate = 注册日期;
this._useMin = useMin;
this._className = 类名;
this._classType = 类类型;
this._percent = 百分比;
}
#endregion
公共属性#region 公共属性
// 公共字符串NoUse
// {
// 获取{return _nouse;}
// 设置{ _nouse= 值;}
// }
公共字符串登录日期
{
获取{返回_loginDate; }
设置 { _loginDate = 值; }
}
公共字符串 LogoutDate
{
获取{返回_logoutDate; }
设置 { _logoutDate = 值; }
}
公共字符串注册日期
{
获取 { 返回 _registerDate; }
设置 { _registerDate = 值; }
}
公共字符串 UseMin
{
获取{返回_useMin; }
设置 { _useMin = 值; }
}
公共字符串类名
{
获取{返回_className; }
放
{
if (值!= null && value.Length > 50)
throw new ArgumentOutOfRangeException("ClassName 的值无效", value, value.ToString());
_className = 值;
}
}
公共字符串类类型
{
获取{返回_classType; }
放
{
if (值!= null && value.Length > 50)
throw new ArgumentOutOfRangeException("ClassType 的值无效", value, value.ToString());
_classType = 值;
}
}
公共字符串 百分比
{
得到{返回_百分比; }
放
{
if (值!= null && value.Length > 50)
throw new ArgumentOutOfRangeException("百分比值无效", value, value.ToString());
_百分比=值;
}
#endregion
http://bluewater.cnblogs.com/archive/2006/06/19/429720.html