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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
Docker
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
美团技术团队
爱范儿
爱范儿
V
V2EX
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
博客园 - 司徒正美
博客园 - 叶小钗
S
SegmentFault 最新的问题
量子位
S
Secure Thoughts
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
L
LINUX DO - 最新话题
罗磊的独立博客
SecWiki News
SecWiki News
雷峰网
雷峰网
Recent Announcements
Recent Announcements
V2EX - 技术
V2EX - 技术
T
Tailwind CSS Blog
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
云风的 BLOG
云风的 BLOG
Schneier on Security
Schneier on Security
T
The Blog of Author Tim Ferriss
IT之家
IT之家
博客园 - 聂微东
腾讯CDC
N
News | PayPal Newsroom
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
aimingoo的专栏
aimingoo的专栏
Webroot Blog
Webroot Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
K
Kaspersky official blog

博客园 - 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;
        }

    }
}