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 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!