Public class U
{public static matchcollection Getmidvalue (string begin, String end, string html) {Regex reg = new Regex ("( ? <= ("+ Begin +")) [. \\s\\s]*? (? = ("+ End +")) ", Regexoptions.multiline | Regexoptions.singleline); Return Reg. Matches (HTML); } static public string GetValue (String str, string start, String end) {Regex RG = new Regex ("? <= (" + STA RT + ")) [. \\s\\s]*? (? = ("+ End +")) ", Regexoptions.multiline | Regexoptions.singleline); Return RG. Match (str). Value; } static public string nohtml (string htmlstring)//Remove HTML tag {//delete script htmlstring = Regex.Replace (htmlst Ring, @ "<script[^>]*?>.*?</script>", "", regexoptions.ignorecase); Delete html htmlstring = Regex.Replace (htmlstring, @ "< (. [ ^>]*) > "," ", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "([\ r]) [\s]+", "", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "-", "", Regexoptions.igNorecase); htmlstring = Regex.Replace (htmlstring, @ "<!--. *", "", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (quot| #34);", "\" ", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (amp| #38);", "&", Regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (lt| #60);", "<", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (gt| #62);", ">", Regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (nbsp| #160);", "", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (iexcl| #161);", "\xa1", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (cent| #162);", "\xa2", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (pound| #163);", "\xa3", regexoptions.ignorecase); htmlstring = Regex.Replace (htmlstring, @ "& (copy| #169);", "\xa9", Regexoptions.ignorEcase); htmlstring = Regex.Replace (htmlstring, @ "(\d+);", "", regexoptions.ignorecase); Htmlstring.replace ("<", ""); Htmlstring.replace (">", ""); Htmlstring.replace ("\ R", ""); return htmlstring; }}
Public class W
{public static string get (String urlstring) {return Getutf8 (urlstring); public static string Getutf8 (String urlstring) {return get (urlstring, Encoding.UTF8); } public static string GETGBK (String urlstring) {return get (urlstring, encoding.getencoding ("GBK")); } public static string getgb2312 (String urlstring) {return get (urlstring, encoding.getencoding ("gb2312")); public static string Get (String urlstring, Encoding Encoding) {//define local variable HttpWebRequest httpwebreq Uest = null; HttpWebResponse httpwebrespones = null; Stream stream = null; String htmlstring = String. Empty; Request page try {HttpWebRequest = WebRequest.Create (urlstring) as HttpWebRequest; }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while creating the page request! ", ex); } httpwebrequest.useragent = "mozilla/4.0 (compatible; MSIE 7.0; WindowsNT 5.1;. NET CLR 2.0.50727; Maxthon 2.0) "; Gets the return information of the server try {httpwebrespones = (HttpWebResponse) httpwebrequest.getresponse (); stream = Httpwebrespones.getresponsestream (); }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while accepting the server's return page! ", ex); } StreamReader StreamReader = new StreamReader (stream, encoding); Read back page try {htmlstring = Streamreader.readtoend (); }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while reading the page data! ", ex); }//Release resource return result Streamreader.close (); Stream. Close (); return htmlstring; }///<summary>//provide a way to get pages via the Post method//</summary>//<param name= "urlstring" > Requested url</ param>//<param name= "encoding" > Page use encoding </param>//<param name= "postdatastring" >post data </p aram>//<returns>Gets the page </returns> public static string post (String urlstring, Encoding Encoding, string postdatastring) { Define local Variables Cookiecontainer Cookiecontainer = new Cookiecontainer (); HttpWebRequest HttpWebRequest = null; HttpWebResponse HttpWebResponse = null; Stream inputstream = null; Stream outputstream = null; StreamReader StreamReader = null; String htmlstring = String. Empty; Convert post data byte[] Postdatabyte = encoding. GetBytes (postdatastring); Create a page request try {HttpWebRequest = WebRequest.Create (urlstring) as HttpWebRequest; }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while creating the page request! ", ex); }//Specify the request processing mode Httpwebrequest.method = "POST"; Httpwebrequest.keepalive = false; Httpwebrequest.contenttype = "application/x-www-form-urlencoded"; Httpwebrequest.cookiecontainer = Cookiecontainer; HttpWebRequest.contentlength = Postdatabyte.length; Transferring data to the server try {InputStream = Httpwebrequest.getrequeststream (); Inputstream.write (postdatabyte, 0, postdatabyte.length); }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while sending the post data! ", ex); } finally {inputstream.close (); }//Accept server return information try {HttpWebResponse = HttpWebRequest.GetResponse () as HttpWebResponse; OutputStream = Httpwebresponse.getresponsestream (); StreamReader = new StreamReader (outputstream, encoding); htmlstring = Streamreader.readtoend (); }//Handling exception catch (Exception ex) {throw new Exception ("An error occurred while accepting the server's return page! ", ex); } finally {streamreader.close (); } foreach (Cookie cookie in httpwebresponse.cookies) {Cookiecontainer.add (cookie); } return htmlstring; }}
C # Network operations class