wiledog 2018-06-09 09:41 采纳率: 100%
浏览 2316
已采纳

C# 为采集网站需首先登陆,请问怎么模拟登陆

网站登陆页面:http://account.aiweibang.com/account/login.html
怎样实现模拟登陆,另外登陆后采集是否需要 cookie, cookie怎么采集,请提供解决方案并完成实际测试。

  • 写回答

6条回答 默认 最新

  • threenewbee 2018-06-09 12:57
    关注
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace Q691615
    {
        class Program
        {
            static void Main(string[] args)
            {
                CookieContainer cookies = new CookieContainer();
                string url = "http://account.aiweibang.com/api/account/account/login_phone";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                request.Method = "POST";
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
                request.CookieContainer = cookies;
                int icurrtime = (int)new TimeSpan(DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks).TotalSeconds;
                string currtime = icurrtime.ToString();
                request.Headers.Add("Cookie: Hm_lvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime + "; Hm_lpvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime);
                string postdata = "phone=136****&password=1*****"; //注意这里的用户名密码需要还原成你的
                byte[] postdataByte = Encoding.UTF8.GetBytes(postdata);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postdataByte.Length;
                Stream st;
                st = request.GetRequestStream();
                st.Write(postdataByte, 0, postdataByte.Length);
                st.Close();
                var response = (HttpWebResponse)request.GetResponse();
                StreamReader readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string context = readd.ReadToEnd();
                response.Close();
                string token = Regex.Match(context, @"(?<=token\""\:\"")[^\""]+").Value;
                url = "http://account.aiweibang.com/api/account/account/info";
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                request.Method = "POST";
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36";
                request.CookieContainer = cookies;
                request.Headers.Add("Cookie: Hm_lvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime + "; Hm_lpvt_45cdb9a58c4dd401ed07126f3c04d3c4=" + currtime + " awbYHQXTK=" + token);
                request.Headers.Add("X-CLIENT:web");
                request.Headers.Add("X-TOKEN:" + token);
                request.Headers.Add("X-VERSION:1.0");
                postdata = "";
                postdataByte = Encoding.UTF8.GetBytes(postdata);
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postdataByte.Length;            
                st = request.GetRequestStream();
                st.Write(postdataByte, 0, postdataByte.Length);
                st.Close();
                response = (HttpWebResponse)request.GetResponse();
                readd = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                context = readd.ReadToEnd();
                response.Close();
                Console.WriteLine(context);
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?