请求网址并解析返回的html
目的,把远程服务器传回的html,解析到类里面,为gridview等提供数据源 info = ""; }2、解析返回的html,有省略 arraylist ar = new arraylist(); ar.add(httputility.htmldecode(m.value)); ci[i].logindate = (ar[i * 8 + 1]).tostring(); } return ci;
1 、向远程服务器post数据
public int postdata(string url, string data, out string info)
{
cookiecontainer cc = new cookiecontainer();
httpwebrequest request = webrequest.create(url) as httpwebrequest;
request.cookiecontainer = cc;
request.method = "post";
request.contenttype = "application/x-www-form-urlencoded";
stream requeststream = request.getrequeststream();
byte[] bytearray = encoding.utf8.getbytes(data);
requeststream.write(bytearray, 0, bytearray.length);
requeststream.close();
httpwebresponse response = request.getresponse() as httpwebresponse;
uri responseuri = response.responseuri;
stream receivestream = response.getresponsestream();
encoding encode = system.text.encoding.getencoding("utf-8");
streamreader readstream = new streamreader(receivestream, encode);
string result = readstream.readtoend();
info = result;
return 0;
public classinfo[] getclass(string html)
{
arraylist arr = new arraylist();
string table = "";
regex regtable = new regex(@"(?<=<table.*>).*?(?=</table>)", regexoptions.singleline);
match ma = regtable.match(html);
while (ma.success)
{
if (ma.value.trim() != "")
{
arr.add(httputility.htmldecode(ma.value));
}
ma = ma.nextmatch();
}
for (int i = 0; i < arr.count; i++)
{
table = arr[i].tostring() + table;
}
regex reg = new regex(@"(?<=<.*?>).*?(?=<.*?>)", regexoptions.singleline);
match m = reg.match(table);
while (m.success)
{
if (m.value.trim() != "")
{
}
m = m.nextmatch();
}
classinfo[] ci = new classinfo[classno];
for (int i = 0; i < classno; i++)
{
ci[i] = new classinfo();
ci[i].registerdate = (ar[i * 8 + 0]).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();
}
Tag:
gridview