惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Troy Hunt's Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News: Ask HN
Hacker News: Ask HN
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Hacker News - Newest:
Hacker News - Newest: "LLM"
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
S
Secure Thoughts
爱范儿
爱范儿
Jina AI
Jina AI
H
Heimdal Security Blog
量子位
罗磊的独立博客
人人都是产品经理
人人都是产品经理
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cisco Talos Blog
Cisco Talos Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
T
Tor Project blog
博客园_首页
T
Tenable Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Latest news
Latest news
AWS News Blog
AWS News Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
Last Week in AI
Last Week in AI
V
Visual Studio Blog
The Hacker News
The Hacker News
I
Intezer
L
LINUX DO - 最新话题
L
LangChain Blog
W
WeLiveSecurity

博客园 - pot

在框架中(IFRAME/FRAMESET)传递COOKIE的解决方案[转] jQuery图片播放插件Fancybox使用方法 C#生成缩略图代码 抓取AJAX网页的方法-Firefox组件,C#集成 C#编号的ActiveX控件采用CAB的布署方式实例 C#编写ActiveX控件实例(包括命令和事件) PdfAcroViewer C#代码登录活动目录 用XML反序列化快速完成ASP.NET配置文件 - pot - 博客园 android Activity类的使用 Android中的Intent详细讲解 Android模拟器常用使用,和基本功能使用 ZPL II 命令参考 正则表达式语法 - pot - 博客园 《C#异常处理》 C#中接口的作用 The RSA key container could not be opened - pot ASP.NET 页面事件执行顺序 关于object sender,EventArgs e 的一些解释
数据抓取的一个类,包含一些常用的方法
pot · 2012-02-29 · via 博客园 - pot

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace XXX
{
    /// <summary>
    
/// Func 的摘要说明。
    
/// </summary>
    public class Func
    {
        public CookieContainer myCookieContainer = new CookieContainer();

        public void SetCookie(string cookieStr, string domain)
        {
            string[] cookstr = cookieStr.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                ck.Domain = domain;
                myCookieContainer.Add(ck);
            }
        }

        public string GetPage(string PageUrl)
        {
            return GetPage(PageUrl, Encoding.Default);
        }

        public string GetPage(string PageUrl, Encoding encoding)
        {
            string backstr = string.Empty;
            try
            {
                //System.Net.WebRequest  request = System.Net.WebRequest.Create(PageUrl);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PageUrl);
                request.CookieContainer = myCookieContainer;

                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
                System.Net.WebResponse response = request.GetResponse();
                System.IO.Stream resStream = response.GetResponseStream();
                resStream.ReadTimeout = 8000;
                System.IO.StreamReader sr = new System.IO.StreamReader(resStream, encoding);
                backstr = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
                backstr = "";
            }
            return backstr;
        }

        public string PostPage(string PageUrl, string postData)
        {
            ASCIIEncoding encoding = new ASCIIEncoding(); 
            byte[] data = encoding.GetBytes(postData);

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PageUrl);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();
            // Send the data. 
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            // Get response 
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
            string content = reader.ReadToEnd();
            reader.Close();
            return content;
        }

        public string[] StrSplit(string mystr, string splitstr)
        {
            string str = mystr.Replace(splitstr, "\x254");
            string[] backstring = str.Split('\x254');
            return backstring;
        }

        public string Cutstr(string mystr, string str1, string str2)
        {
            string backstr = string.Empty;
            int strstart = 0;
            int strend = mystr.Length;

            if (str1 != "")
                strstart = mystr.IndexOf(str1);
            if (str2 != "")
                strend = mystr.IndexOf(str2, strstart + str1.Length);
            if (strstart != -1 && strend != -1)
                backstr = mystr.Substring(strstart + str1.Length, strend - strstart - str1.Length);
            else
                backstr = "-1";
            return backstr;
        }

        public string ReplaceFirst(string mystr, string oldstr, string newstr)
        {
            string backstr = mystr;
            int oldindex = mystr.IndexOf(oldstr);
            if (oldindex > -1)
            {
                backstr = backstr.Remove(oldindex, oldstr.Length);
                backstr = backstr.Insert(oldindex, newstr);
            }
            return backstr;
        }

        public void writetxt(string pathstr, string content)
        {
            FileStream fs = new FileStream(pathstr, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.Write(content);
            sw.Close();
            fs.Close();
        }

        public string readtxt(string pathstr)
        {
            string tmpstr = string.Empty;
            FileStream fs = new FileStream(pathstr, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            tmpstr = sr.ReadToEnd();
            sr.Close();
            fs.Close();
            return tmpstr;
        }

        public string FilterLink(string str)
        {
            string tmpstr = str;
            Regex re = new Regex(@"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            MatchCollection mc = re.Matches(tmpstr);
            for (int i = 0; i < mc.Count; i++)
            {
                tmpstr = tmpstr.Replace(mc[i].Value, mc[i].Groups["text"].Value);
            }
            return tmpstr;
        }

    }
}