目的,把遠端伺服器傳回的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[類];
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