首页
社区
课程
招聘
[求助]用send发送登陆的http数据包登陆,那么此时登陆后的cookie怎么获取呢?
发表于: 2008-12-6 16:49 19725

[求助]用send发送登陆的http数据包登陆,那么此时登陆后的cookie怎么获取呢?

2008-12-6 16:49
19725
收藏
免费 0
支持
分享
最新回复 (6)
雪    币: 231
活跃值: (45)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
qdk
2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;

namespace test2
{
    class Program
    {
        //百度网页的编码格式为GB2312
        static Encoding encoding = Encoding.GetEncoding("GB2312");
        static String cookieStr;//保存cookie字符串用的

        static Thread[] threadGrp;
        static int thread_num =10;//线程数目
        static int indexer = 0;   //代理索引
        static int urlIndex = 0;  //需要访问的链接的索引
            

        static int normal_sleep =2000;//每次访问之后线程休眠的时间,刷得太快会被封ip
        static int failed_sleep =0;

        static int counter = 0;

        static int majorCounter=0;//记录总共访问了多少次

        static bool ifUseProxy = false;//是否启用代理,用了代理访问速度会变慢,需要注意的是,baidu能够认出普通的代理

        static void Main(string[] args)
        {
            //从文件中获取上一次的session cookie或者重新获取session cookie
            FileStream fs = new FileStream("C:\\ss.txt",FileMode.OpenOrCreate);
            if (fs.Length>0)
            {
                //如果文件是空或者没有文件,则重新获取session cookie
                StreamReader sr = new StreamReader(fs);
                cookieStr = sr.ReadToEnd();
                //将新的cookie写到文件中,这样做的目的是不必每次启动程序就去登录获取cookie,次数多了之后,会需要验证码
                //程序还没有实现验证码识别
                Console.WriteLine("Get Session from file:\n" + cookieStr);
                sr.Close();
                fs.Close();
            }
            else
            {
                //从文件中直接获取上次的cookie
                StreamWriter sw = new StreamWriter(fs);
                cookieStr = getSession();
                Console.WriteLine("Get new Session:\n" + cookieStr);
                sw.Write(cookieStr);
                sw.Close();
                fs.Close();
            }
            //线程的初始化
            threadGrp = new Thread[thread_num];
            for (int i = 0; i < thread_num; i++)
            {
                threadGrp[i] = new Thread(new ThreadStart(ThreadProc));
                threadGrp[i].Start();
            }
            //线程一直运行
            for (int i = 0; i < thread_num; i++)
            {
                threadGrp[i].Join(Timeout.Infinite);
            }
        }

/// <summary>
/// 访问线程
/// </summary>

        static void ThreadProc()
        {
            
            while (true)
            {
                //获取要访问的url
                if (urlIndex >= urlList.Length) urlIndex = 0;
                int localUrlIndex = urlIndex++;
                int localMajorCounter = majorCounter++;
                //获取代理
                if (indexer >= ipList.Length) indexer = 0;
                String proxy_url = ipList[indexer++];
                String vUrl = urlList[localUrlIndex];
                try
                {
                    //代理
                    WebProxy wp = new WebProxy();
                    if (ifUseProxy) wp.Address = new Uri("http://" + proxy_url, UriKind.Absolute);
                    else
                    {
                        wp = null;
                        proxy_url = "no proxy";
                    }

                    Console.WriteLine("urlIndex:" + localUrlIndex + "--" + visitUrl(vUrl, wp, cookieStr) + "---" + localMajorCounter + "--" + proxy_url);
                    counter++;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("urlIndex:" + localUrlIndex + "--failed----" + proxy_url);
                    Console.Beep(1000, 10);
                    Thread.Sleep(failed_sleep);
                }
                //休眠
                Thread.Sleep(normal_sleep);
            }
        } 
        static String visitUrl(String url, WebProxy wbp,String cookieStr)
        {
            HttpWebResponse resp=null;
            try
            {
                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
                hwr.KeepAlive = false;
                hwr.Timeout = 5000;
                hwr.Method = "GET";
                hwr.Proxy = wbp;
                //添加cookie,需要cookie操作才会生效
                hwr.Headers.Add(HttpRequestHeader.Cookie, cookieStr);
                resp = (HttpWebResponse)hwr.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    return "ok";
                }
                else return "error";
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                if (resp != null) resp.Close();
            }
        }
        static String getSession()
        {
            //链接到登录链接获取会话cookie                                                 //下面这里改成你的密码
            String loginUrl = "http://passport.baidu.com/?login&username=quicker1&password=***&mem_pass=1";
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(loginUrl);
            CookieContainer ck = new CookieContainer();
            hwr.CookieContainer = ck;
            HttpWebResponse resp = null;
            try
            {

                resp = (HttpWebResponse)hwr.GetResponse();
                if (resp.StatusCode == HttpStatusCode.OK)
                {
                    return ck.GetCookieHeader(new Uri("http://www.baidu.com"));
                }
                else return "error";
            }
            catch (Exception ex)
            {
                return "error";
            }
        }
        //代理
        static String[] ipList = new String[] {"62.116.163.221:80",
                                                /*"way-cache.cache.telstra.net",*/
                                                /*"wel-cache.cache.telstra.net",*/
                                               /* "67.69.254.247:80",*/
                                                "58.227.194.87:8080",
                                               "200.206.46.178:3128",
                                                "222.251.133.59:8080",
                                                "66.198.41.11:3128",
                                               /* "203.162.183.222:80",*/
                                                "200.51.41.29:8080"
                                            };
        //url链接
        static String[] urlList = new String[]{"http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011",
            "http://tieba.baidu.com/f?kz=488742011"
            /*
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4855029380&rs5=2566640443&rs6=699254329",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861926426&rs5=2526935261&rs6=688662754",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861927273&rs5=2563697529&rs6=711733387",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861981695&rs5=2595998963&rs6=855062725",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861980899&rs5=2626507173&rs6=879515016",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861980141&rs5=2464481818&rs6=747027366",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861992874&rs5=2596957230&rs6=875513198",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861991861&rs5=2544832356&rs6=858427489",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861991160&rs5=2502710187&rs6=783159987",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4861989942&rs5=2638347912&rs6=860318259",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4862537395&rs5=2606063447&rs6=736172888",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4862536028&rs5=2522080370&rs6=642131497",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4862534930&rs5=2478264039&rs6=726966229",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4862531644&rs5=2468432760&rs6=701880288",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4862528721&rs5=2512750996&rs6=694250489",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863341986&rs5=2558087612&rs6=784727635",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863340081&rs5=2457239537&rs6=678896336",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863325425&rs5=2492746799&rs6=654977541",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863339063&rs5=2568871766&rs6=674937449",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863337309&rs5=2537381625&rs6=654999945",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863324275&rs5=2527607725&rs6=676785280",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863173705&rs5=2488523109&rs6=724718179",
            "http://tieba.baidu.com/f?ct=369098752&cm=39&tn=baiduManagerSubmit&word=%CB%E6%BB%FA&lm=1017580&z=483983646&sc=4863020918&rs5=2439099200&rs6=659471219"
             */
        };

    }
}


2008-12-6 17:03
0
雪    币: 8835
活跃值: (2404)
能力值: ( LV12,RANK:760 )
在线值:
发帖
回帖
粉丝
3
太复杂了,直接手工分析就行了~~
http协议很简单的说~
2008-12-7 19:30
0
雪    币: 211
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
不是可以直接读取Cookie吗?
2008-12-7 22:01
0
雪    币: 424
活跃值: (1829)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
5
分析recv接收到的数据,以Cookie:开头的那行就是了
发送http建议用WinHTTP,用Socket比较麻烦
2008-12-8 08:44
0
雪    币: 590
活跃值: (177)
能力值: ( LV9,RANK:680 )
在线值:
发帖
回帖
粉丝
6
直接用抓包工具抓取后分析,简单
2008-12-8 08:55
0
雪    币: 796
活跃值: (370)
能力值: ( LV9,RANK:380 )
在线值:
发帖
回帖
粉丝
7
我用InternetGetCookie来弄了。。。

感谢各位
2008-12-8 21:29
0
游客
登录 | 注册 方可回帖
返回
//