用cookie模拟登陆

request.Headers["Cookie"] = "Cookie";

<add key="cookie" value="CNZZDATA3622460=cnzz_eid=63363261-1341209998-&amp;ntime=1341283104&amp;cnzz_a=0&amp;retime=1341283103667&amp;sin=&amp;ltime=1341283103667&amp;rtime=1; __utma=247169154.2002541148.1341210000.1341220883.1341283103.3; __utmz=247169154.1341210000.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_b96f95878b55be2cf49fb3c099aea393=1341283103918; _gscu_468690817=412100007lfhzi40; rememberusername=true; username=dwnljcg; PHPSESSID=otm61lj3t7sv73nafn8ddr75s4; __utmb=247169154.1.10.1341283103; __utmc=247169154; Hm_lpvt_b96f95878b55be2cf49fb3c099aea393=1341283103918; _gscs_468690817=41283104pvez3s18|pv:1; _gscbrs_468690817=1"/>

手工登陆一下,然后获取cookie,然后如上所示放到app.config中 读取到reques.Headers["cookie"]=your cookie
如何获取cookie?firebug里可以找到文件头,从文件头里找到cookie

 

获取cookie的代码(未测试)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Sina_DianXin_Wap
{
    class GetCookie
    {
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);

        public static string GetCookies(string url)
        {
            uint datasize = 256;
            StringBuilder cookieData = new StringBuilder((int)datasize);

            if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
            {

                if (datasize < 0)
                    return null;
                cookieData = new StringBuilder((int)datasize);

                if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))

                    return null;
            }
            return cookieData.ToString();
        }
    }
}

posted on 2012-07-09 15:18  雨渐渐  阅读(748)  评论(0编辑  收藏  举报

导航