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条)

报告相同问题?

悬赏问题

  • ¥20 求一个会修改Javaweb 代码的,非常简单
  • ¥20 有没有会写mysql的,有偿做个问题
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥15 VB.NET2022如何生成发布成exe文件
  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题