网站抓包如下:
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; 4399Box.720; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; AE8)
Host: login.jiayuan.com
Content-Length: 70
Connection: Keep-Alive
Pragma: no-cache
Cookie: stadate1=18771161; myloc=31%7C3115; myage=28; mysex=m; myuid=18771161; myincome=40; SESSION_HASH=4e836c0f94c7ff79dd0e61ecd5139ec23ba409b3; SEARCH_CUSTOM=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his1=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; pop_1324358704=1369680546406; sear_his2=f%7C18%7C19%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his3=f%7C0%7C0%7C155%7C170%7C0%7C0%7C0%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; save_jy_login_name=wrzero%40163.com; last_login_time=1369754176; sl_jumper=%26cou%3D17%26omsg%3D0%26dia%3D0%26lst%3D2013-05-28; mylevel=1; registeruid=19771161; PHPSESSID=941c3af6081f3ac81dbabff5f44ae4f6; main_search:19771161=%7C%7C%[email protected]&password=EA2S897GIe5&ljg_login=1&channel=0&position=0代码利用httpwebquest post 自动登陆并且获得cookie,代码如下,但是总是无法正确的post数据上去。网站没有反应:
  public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            try
            {
                CookieContainer cc = new CookieContainer();
                request = (HttpWebRequest)WebRequest.Create(loginUrl);
                request.Method = header.method;
                request.ContentType = header.contentType;
                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabyte.Length;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabyte, 0, postdatabyte.Length);
                stream.Close();                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);                CookieCollection cook = response.Cookies;
                //Cookie字符串格式
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);                return cc;
            }
            catch (Exception ex)
            {                throw ex;
            }
        } public class HttpHeader
        {
            public string contentType { get; set; }            public string accept { get; set; }            public string userAgent { get; set; }            public string method { get; set; }            public int maxTry { get; set; }
        }调用函数:
  HttpHeader header = new HttpHeader();
            header.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            header.contentType = "application/x-www-form-urlencoded";
            header.userAgent = " Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 ";
            header.method = "POST";GetCooKie("http://login.jiayuan.com/dologin.php",                      "[email protected]&password=EA2S897GIe5&ljg_login=1&channel=0&position=0", header);是post数据不对?

解决方案 »

  1.   

    应该指定数据格式吧,是xml格式还是json,然后post的数据格式应该和这个保持一致
      

  2.   

    抓包没有烤全,应该是xml个格式的:
    Host: login.jiayuan.com
    Connection: keep-alive
    Content-Length: 78
    Cache-Control: max-age=0
    Origin: http://login.jiayuan.com
    User-Agent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0
    Content-Type: application/x-www-form-urlencoded
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Referer: http://login.jiayuan.com/?pre_url=http%3A%2F%2Fusercp.jiayuan.com%2F&refrer=http://www.jiayuan.com&host=0
    Accept-Encoding: gzip,deflate
    Accept-Language: zh-CN,zh;q=0.8
    Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
    Cookie: pgv_pvi=6096739328; jy_ztlogin_springdate_18771161=springdate; jy_ztlogin_llkan_18771161=llkan; jy_ztlogin_dafuweng_18771161=dafuweng; jy_ztlogin_zadan_18771161=zadan; save_jy_login_name=wrzero%40163.com; sear_his1=f%7C0%7C0%7C155%7C170%7C0%7C0%7C0%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his2=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his3=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; SEARCH_CUSTOM=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; SESSION_HASH=80b8c8ac591d32ef91b32eb8b8b5db4b0b848328; registeruid=19771161; pop_1324358704=1370018666334; pop_time=1370008500872; REG_REF_URL=; sl_jumper=%26cou%3D17%26omsg%3D0%26dia%3D0%26lst%3D2013-05-28; last_login_time=1370008939; IM_CS=2; IM_ID=2; pop_1361757808=1370023353930; pclog=%7B%2219771161%22%3A%221370005466547%7C0%7C0%22%2C%2292525078%22%3A%221370008954455%7C0%7C0%22%7D; IZ_bind92525078=0; IM_CON=%7B%22IM_TM%22%3A1370008954809%2C%22IM_SN%22%3A1%7D; IM_S=%7B%22IM_CID%22%3A8773519%2C%22svc%22%3A%7B%22code%22%3A0%2C%22nps%22%3A0%2C%22unread_count%22%3A%221%22%2C%22ocu%22%3A0%2C%22ppc%22%3A0%2C%22jpc%22%3A0%2C%22using%22%3A%22%22%2C%22uid%22%3A92525078%7D%2C%22m%22%3A0%2C%22f%22%3A0%2C%22omc%22%3A0%7D; poplogincount=1; IM_TK=1370008959911; IM_M=%5B%5D; stadate1=91525078; myloc=37%7C3702; myage=22; mysex=m; myuid=91525078; myincome=10; main_search:92525078=%7C%7C%7C00; PHPSESSID=9b4f452e5f564fb78bae92418e7f2ce2name=7OT691XGZ%40163.com&password=EA2S897GIe5&ljg_login=1&channel=0&position=0
      

  3.   

    是XML格式就指定post的contentType为application/xml格式试试
      

  4.   

    改了更错了,cookie都是这个样子:HTTP/1.1 200 OK
    Server: nginx
    Date: Sun, 02 Jun 2013 14:44:17 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    Set-Cookie: PHPSESSID=ec17c4cdbeaccf58f20064113ca7d4ed; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    P3P: CP="CAO PSA OUR"72
    <script type='text/javascript'>top.location.href='http://login.jiayuan.com/err.php?err_type=-7&pre_url=';</script>
    0
    header.contentType = "application/x-www-form-urlencoded";这个是对的:HTTP/1.1 200 OK
    Server: nginx
    Date: Sun, 02 Jun 2013 14:46:47 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    Set-Cookie: PHPSESSID=5597ec119b541e3d1a9a8552b745f68a; path=/
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Pragma: no-cache
    P3P: CP="CAO PSA OUR"
    Set-Cookie: stadate1=91525078; expires=Sat, 31-Aug-2013 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: myloc=37%7C3702; expires=Mon, 02-Jun-2014 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: myage=22; expires=Mon, 02-Jun-2014 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: PROFILE=92525078%3A%25E6%259B%25BE%25E6%25AA%2580%25E9%259B%2585%3Am%3Aphotos1.jiayuan.com%2F01%2F07%2Ff5e8aa795d55a324f8a9c672366d%3A0%3A%3Ahttp%3A%2F%2Fimages1.jyimg.com%2Fw4%2Fglobal%2Fi%2Fnopic_m.jpg%3A1; path=/; domain=jiayuan.com
    Set-Cookie: mysex=m; expires=Mon, 02-Jun-2014 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: myuid=91525078; expires=Mon, 02-Jun-2014 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: myincome=10; expires=Mon, 02-Jun-2014 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: main_search:92525078=%7C%7C%7C00; path=/
    Set-Cookie: bl_id=deleted; expires=Sat, 02-Jun-2012 14:46:46 GMT; path=/
    Set-Cookie: SESSION_HASH=b0478d8fdb81178ff3c3c5eae8b9a689a3aa7e9a; expires=Mon, 03-Jun-2013 00:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: RAW_HASH=bee9b6151c3cc9e1c37d8988b7d1a61fc4ca6ab7; path=/; domain=jiayuan.com
    Set-Cookie: COMMON_HASH=01f5e8aa795d55a324f8a9c672366d07; path=/; domain=jiayuan.com
    Set-Cookie: sl_jumper=%26cou%3D17%26omsg%3D0%26dia%3D0%26lst%3D2013-06-02; expires=Sun, 02-Jun-2013 15:16:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: last_login_time=1370184407; expires=Mon, 03-Jun-2013 14:46:47 GMT; path=/; domain=jiayuan.com
    Set-Cookie: registeruid=92525078; expires=Mon, 10-Jun-2013 02:46:47 GMT; path=/172
    <script type='text/javascript' src='http://jiayuan.msn.com.cn/login/synlogin.php?rct=4TBf1tVyOAteG2XKK2qnX4Iiad3VYwlPkYMByEHJ8gu8fAZ6zYSERMXXftrPRF7r%2AcO%2AQy6zrTFywfcsgbYRbtYp2CPva%2AsZNtxQ3RY52WzpD7-g4qoSvdnJUoAy0RC-pBGicl0n5bszqD84X5raVUQl950.&ticket=8cecec'></script><script type='text/javascript'>top.location.href='http://www.jiayuan.com/usercp#maodian';</script>
    0
    里面的返回的cookie还是对的。就是后面没了,还是没登陆进去。
      

  5.   

    按照你所谓“抓包”结果看,人家是先访问了网站,进行了一些操作并且设置了cookie,之后才post。
      

  6.   

    POST /dologin.php?host=www.jiayuan.com&new_header=1&channel=index HTTP/1.1
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/QVOD, application/QVOD, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-xpsdocument, */*
    Referer: http://www.jiayuan.com/
    Accept-Language: zh-cn
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; 4399Box.720; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; AE8)
    Host: passport.jiayuan.com
    Content-Length: 68
    Connection: Keep-Alive
    Pragma: no-cache
    Cookie: SESSION_HASH=38dc50c6205d35c8bc9511651286e4f9e2aff556channel=200&position=201&[email protected]&password=EA2S897GIe5
      

  7.   

    应该是只有1个seesion_hash,第一次发送出去的时候,在cookie里面。
      

  8.   

    seesion_hash,这个是第一次提交后返回的响应里得到的啊。第一次发送出去,怎么得到这个值啊。
      

  9.   

    检查下你提交的cookie是不是有问题。
    按照这种格式一起提交COOKIE看看
    cc.Add(new Uri(PostURL), new Cookie("SEARCH_CUSTOM", "f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his1=f%7C18%7C27%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; pop_1324358704=1369680546406; sear_his2=f%7C18%7C19%7C155%7C170%7C0%7C0%7C1%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0; sear_his3=f%7C0%7C0%7C155%7C170%7C0%7C0%7C0%7C1%7C0%7C1%7C0%7C0%7C0%7C0%7C0%7C31%7C3100%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0%7C0"));
      

  10.   

    回上楼,第一次贴的cookie不对,是之前浏览器的数据,清空所有cookie后,再次抓包登陆。发现cookie里面只有这个:
    Cookie: SESSION_HASH=38dc50c6205d35c8bc9511651286e4f9e2aff556
      

  11.   

     public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
            {
                HttpWebRequest request = null;
                HttpWebResponse response = null;
                CookieContainer cc = new CookieContainer();
                request = (HttpWebRequest)WebRequest.Create(loginUrl);
                request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabyte.Length;
                request.CookieContainer = cc;
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabyte, 0, postdatabyte.Length);
                stream.Close();
                //接收响应          
                response = (HttpWebResponse)request.GetResponse();
                response.Close();
                return cc;
            }
            public static string GetStr(string url, CookieContainer cc)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cc;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string retStr;
                using (Stream str = response.GetResponseStream())
                {
                    StreamReader sr = new StreamReader(str);
                    retStr = sr.ReadToEnd();
                }
                response.Close();
                return retStr;
            }
           //CookieContainer cc= GetCooKie("http://login.jiayuan.com/dologin.php", "[email protected]&password=EA2S897GIe5&ljg_login=1&channel=0&position=0", null)
           // string str=   GetStr("http://login.jiayuan.com",cc);
    我照你的要求测了一下可以自动登陆的哇
      

  12.   

    你怎么看到登陆成功的啊?肯定不对呢。session id都没有。登陆上去了,也操作不了什么东西。
      

  13.   

    重新抓了下包,应该是先访问www.jiayuan.com。然后得到一个seesion_hash,然后再加到cookie里面提取用户名。post。
    但是第一步get 访问www.jiayuan.com的时候,返回服务器超时:
            public static CookieContainer sessionHash(string getUrl, HttpHeader header)
            {
                HttpWebRequest httpWebRequest = null;
                HttpWebResponse httpWebResponse = null;
                try
                {
                    CookieContainer cc = new CookieContainer();
                    httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
                   // httpWebRequest.ContentType = header.contentType;
                //    httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
               //     httpWebRequest.Referer = getUrl;
                    httpWebRequest.Accept = header.accept;
                 //   httpWebRequest.UserAgent = header.userAgent;
                    httpWebRequest.Method = "GET";
              //      httpWebRequest.Host = "https://www.jiayuan.com";
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    Stream responseStream = httpWebResponse.GetResponseStream();
                    StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                    string html = streamReader.ReadToEnd();
                    httpWebRequest.CookieContainer = cc;
                    streamReader.Close();
                    responseStream.Close();
                    httpWebRequest.Abort();
                    httpWebResponse.Close();                return cc;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }            CookieContainer cookie = new CookieContainer();
                HttpHeader header = new HttpHeader();
                header.accept = "*/*";
              //  header.contentType = "application/x-www-form-urlencoded";
                header.userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; 4399Box.720; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; AE8)";
                cookie = sessionHash("https://www.jiayuan.com/general/?m=oauthDisplayCb&domid=oauthDisplayCb&cb=display_div", header);
      

  14.   

    登陆http://login.jiayuan.com/dologin.php后序列化到文件的cookie
    PHPSESSID=775f28e72544a0c96c8c833ad8ea0674;这块内容相同last_login_time=1370586323
    用前面的cookie登陆http://login.jiayuan.com后变成以下的cookie
    PHPSESSID=775f28e72544a0c96c8c833ad8ea0674; 这块内容相同last_login_time=1370652527; SESSION_HASH=9d8ba8c6a30f33825ba0cfe408bb2df29939ce0a; new_msg=0
    不是很合逻辑吗,不知你要操作的是什么
      

  15.   

    SESSION_HASH=9d8ba8c6a30f33825ba0cfe408bb2df29939ce0a 这个东西是服务器返回的,登陆是cookie里面需要post上去的。所以必须先得到这个值。
      

  16.   

    为啥,get的时候,总是提示远程服务器错误,504。网关超时
      

  17.   


    红圈中的几个值,有人认识的吗?怎么获取到的。抓包,之前的消息里面,返回的值里面也没有啊。ip_loc可以通过ip计算。其他几个怎么得到的?