Asp. NET tip: Request URLs and parse back the Html_ practical tips

Source: Internet
Author: User

Purpose, the remote server back to the HTML, parsing into the class, for the GridView, such as the provision of data sources

1. Post data to remote server

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, parse the returned HTML, there are omitted

Public classinfo[] GetClass (string html)
{
ArrayList ar = new ArrayList ();
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 ()!= "")
{
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, the Definition ClassInfo Class (course Class), uses the Codesmith generation

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 classty PE, 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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.