asp.net 解析html,ASP.NET技巧:请求网址并解析返回的html

目的,把远程服务器传回的html,解析到类里面,为gridview等提供数据源

1 、向远程服务器post数据

public int postdata(string url, string data, out string info)

{

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;

}

2、解析返回的html,有省略

public classinfo[] getclass(string html)

{

arraylist ar = new arraylist();

arraylist arr = new arraylist();

string table = "";

regex regtable = new regex(@"(?<=

).*?(?=)", 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() != "")

{

ar.add(httputility.htmldecode(m.value));

}

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].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();

}

return ci;

}

3、定义classinfo类(课程类),用codesmith生成

public class classinfo

{

member variables#region member variables

protected string _logindate;

protected string _logoutdate;

protected string _registerdate;

protected string _usemin;

protected string _classname;

protected string _classtype;

protected string _percent;

//protected string _nouse;

#endregion

constructors#region constructors

public classinfo() { }

public classinfo(string logindate, string logoutdate, string registerdate, string usemin, string classname, string classtype, string percent)

{

this._logindate = logindate;

this._logoutdate = logoutdate;

this._registerdate = registerdate;

this._usemin = usemin;

this._classname = classname;

this._classtype = classtype;

this._percent = percent;

}

#endregion

public properties#region public properties

//        public string nouse

//        {

//            get{return _nouse;}

//            set{ _nouse= value;}

//        }

public string logindate

{

get { return _logindate; }

set { _logindate = value; }

}

public string logoutdate

{

get { return _logoutdate; }

set { _logoutdate = value; }

}

public string registerdate

{

get { return _registerdate; }

set { _registerdate = value; }

}

public string usemin

{

get { return _usemin; }

set { _usemin = value; }

}

public string classname

{

get { return _classname; }

set

{

if (value != null && value.length > 50)

throw new argumentoutofrangeexception("invalid value for classname", value, value.tostring());

_classname = value;

}

}

public string classtype

{

get { return _classtype; }

set

{

if (value != null && value.length > 50)

throw new argumentoutofrangeexception("invalid value for classtype", value, value.tostring());

_classtype = value;

}

}

public string percent

{

get { return _percent; }

set

{

if (value != null && value.length > 50)

throw new argumentoutofrangeexception("invalid value for percent", value, value.tostring());

_percent = value;

}

}

#endregion

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值