wiledog
wiledog
采纳率100%
2018-06-09 09:41

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

80

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 3年前
     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);
            }
        }
    }
    
    
    点赞 3 评论 复制链接分享
  • fireman1678 九月~前嗅大数据 2年前

    你可以用前嗅的ForeSpider采集器帮你实现登录操作,直接在这个采集器里采集数据,比其他的方式都要简单一些,如果你不想用可视化采集,也是可以直接用代码语言编写的。

    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 3年前

    在C#中,可以使用HttpWebRequest进行相关的模拟登陆,登陆后进行相关的操作,比如抓取数据,页面分析,制作相关登陆助手等等。

    先说下流程

    1.使用httpwebrequest先进入你要登录的网站,获取cookie

    2.使用第一步获取的cookie到验证码的网页将验证码下载下来。

    3.使用Post数据 发送至网站。如果有cookie则继续保存。

    4.使用第三步的cookie登陆相关网页操作。

    获取相关数据可以使用抓包工具进行抓取,如httpwatch。(网上下载的好多都有病毒,下载的时候注意点)

    1。

    ///
    /// 通过get方式请求页面,传递一个实例化的cookieContainer
    ///
    ///
    ///
    ///
    public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
    {
    HttpWebRequest request;
    HttpWebResponse response;
    ArrayList list = new ArrayList();
    request = WebRequest.Create(postUrl) as HttpWebRequest;
    request.Method = "GET";
    request.UserAgent = "Mozilla/4.0";
    request.CookieContainer = cookie;
    request.KeepAlive = true;
    request.CookieContainer = cookie;
    try
    {
    //获取服务器返回的资源
    using (response = (HttpWebResponse)request.GetResponse())
    {
    using (StreamReader reader =
    new StreamReader(response.GetResponseStream(), Encoding.Default))
    {
    cookie.Add(response.Cookies);
    //保存Cookies
    list.Add(cookie);
    list.Add(reader.ReadToEnd());
    list.Add(Guid.NewGuid().ToString());//图片名
    }
    }
    }
    catch (WebException ex)
    {
    list.Clear();
    list.Add("发生异常/n/r");
    WebResponse wr = ex.Response;
    using (Stream st = wr.GetResponseStream())
    {
    using (StreamReader sr =new StreamReader(st, System.Text.Encoding.Default))
    {
    list.Add(sr.ReadToEnd());
    }
    }
    }
    catch (Exception ex)
    {
    list.Clear();
    list.Add("5");
    list.Add("发生异常:" + ex.Message);
    }
    return list;
    }

    2.下载验证码,保存在本地。

    view plaincopy to
    clipboardprint?
    ///
    /// 下载验证码图片并保存到本地
    ///
    /// 验证码URL
    /// Cookies值
    /// 保存位置/文件名
    public staticbool DowloadCheckImg(string Url, CookieContainer cookCon,string
    savePath)
    {
    bool bol =
    true;
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
    //属性配置
    webRequest.AllowWriteStreamBuffering = true;
    webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
    webRequest.MaximumResponseHeadersLength = -1;
    webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, /";
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Method = "GET";
    webRequest.Headers.Add("Accept-Language","zh-cn");
    webRequest.Headers.Add("Accept-Encoding","gzip,deflate");
    webRequest.KeepAlive = true;
    webRequest.CookieContainer = cookCon;
    try
    {
    //获取服务器返回的资源
    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
    using (Stream sream = webResponse.GetResponseStream())
    {
    List list = new List();
    while (true)
    {
    int data = sream.ReadByte();
    if (data == -1)
    break;
    list.Add((byte)data);
    }
    File.WriteAllBytes(savePath, list.ToArray());
    }
    }
    }
    catch (WebException ex)
    {
    bol = false;
    }
    catch (Exception ex)
    {
    bol = false;
    }
    return bol;
    }

    3。发送post数据

    view plaincopy to
    clipboardprint?
    ///
    /// 发送相关数据至页面
    /// 进行登录操作
    /// 并保存cookie
    ///
    ///
    ///
    ///
    ///
    public static ArrayList PostData(string postData,string postUrl, CookieContainer
    cookie)
    {
    ArrayList list = new ArrayList();
    HttpWebRequest request;
    HttpWebResponse response;
    ASCIIEncoding encoding = new ASCIIEncoding();
    request = WebRequest.Create(postUrl) as HttpWebRequest;
    byte[] b = encoding.GetBytes(postData);
    request.UserAgent = "Mozilla/4.0";
    request.Method = "POST";
    request.CookieContainer = cookie;
    request.ContentLength = b.Length;
    using (Stream stream = request.GetRequestStream())
    {
    stream.Write(b, 0, b.Length);
    }
    try
    {
    //获取服务器返回的资源
    using (response = request.GetResponse()as HttpWebResponse)
    {
    using (StreamReader reader =
    new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
    if (response.Cookies.Count > 0)
    cookie.Add(response.Cookies);
    list.Add(cookie);
    list.Add(reader.ReadToEnd());
    }
    }
    }
    catch (WebException wex)
    {
    WebResponse wr = wex.Response;
    using (Stream st = wr.GetResponseStream())
    {
    using (StreamReader sr =
    new StreamReader(st, System.Text.Encoding.Default))
    {
    list.Add(sr.ReadToEnd());
    }
    }
    }
    catch (Exception ex)
    {
    list.Add("发生异常/n/r"+ex.Message);
    }
    return list;
    }

    4。就是第三步请求的链接地址换一个就行了

    好了

    以上核心代码已经贴出了

    具体实现需要靠你们按照你们自己的逻辑

    点赞 评论 复制链接分享
  • wodexinhao wodexinhao 3年前

    我想请问一下 caozhy ,你作为版主怎么可以随便删除别人的恢复,如果涉嫌 复制、灌水或者广告,那情有可原,如果不是,那为何随便删除,是否是触及了你的利益,因为如果是回答有误,你可以指正,也没必要删除,我想请你给个解释,为防止再被随意删除,截图为证

    点赞 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 3年前

    图片说明
    这是成功登录,并且获取到了用户的注册时间等信息。说明登录成功了。

    点赞 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 3年前

    私信给我一个用户名密码,帮你写。

    点赞 评论 复制链接分享

为你推荐