http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 6699|回复: 9

[学生提问] 求助,模拟登陆学校图书馆系统

[复制链接]
发表于 2015-3-20 01:02:54 | 显示全部楼层 |阅读模式
8金钱
我是菜鸟,只求成功登录并得到个人信息。主要卡在cookie,望指教!

地址:(无验证码)http://210.38.138.1:81/test.aspx
(有验证码)http://210.38.138.1:81/login.aspx


帐号:201211621426
密码:qwaszx




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-3-20 08:20:33 | 显示全部楼层
先访问一下登录页面获取一个页面的Cookie然后带上这个Cookie再去Post就行了。

[C#] 纯文本查看 复制代码
  HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "http://210.38.138.1:81/test.aspx",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
                Cookie = "ASP.NET_SessionId=d0m10vqgkt25avbz50k1wp45",//字符串Cookie     可选项   
                Referer ="http://210.38.138.1:81/test.aspx",//来源URL     可选项   
                Postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTY2Njg2ODg0Mw9kFgICAw9kFgQCAw9kFgRmDw9kFgIeDGF1dG9jb21wbGV0ZQUDb2ZmZAIEDw8WAh4EVGV4dGVkZAIFD2QWBmYPEGRkFgFmZAIBDxBkZBYBZmQCAg8PZBYCHwAFA29mZmRknCx%2BbCG6n8kWD2lyI1vGtVNinZg%3D&__EVENTVALIDATION=%2FwEWBQLkl67yDQLxkMjADwLfkqekBgLN05%2BfBgKe9OnfBtV9Y005b7X1Z1zae8A1XKnU4DHq&txtlogintype=0&txtUsername_Lib=201211621426&txtPas_Lib=qwaszx&btnLogin_Lib=%E7%99%BB%E5%BD%95",//Post数据     可选项GET时不需要写 
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值   

            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;


这个就是我说的提前获取一次
Cookie = "ASP.NET_SessionId=d0m10vqgkt25avbz50k1wp45",//字符串Cookie 可选项
回复

使用道具 举报

 楼主| 发表于 2015-3-20 10:30:28 | 显示全部楼层
我就是在首次GET获取页面时,获取到的却不是登录页面,不知道是不是网站设置了防护……

[C#] 纯文本查看 复制代码
 HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() { URL = "http://210.38.138.1:81/test.aspx" };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;


获得的html:
[HTML] 纯文本查看 复制代码
<html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="%2fdefaulterror.aspx%3faspxerrorpath%3d%2ftest.aspx">here</a>.</h2> </body></html>
回复

使用道具 举报

 楼主| 发表于 2015-3-20 10:34:33 | 显示全部楼层
@站长苏飞

感谢站长受教,但是我已经试过这个方法,但就是在首次GET获取页面时,获取到的却不是登录页面,不知道是不是网站设置了防护……可是以前有人是成功登录进去的

[C#] 纯文本查看 复制代码
 HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem() { URL = "http://210.38.138.1:81/test.aspx" };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;


获得的html:
[HTML] 纯文本查看 复制代码
<html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="%2fdefaulterror.aspx%3faspxerrorpath%3d%2ftest.aspx">here</a>.</h2> </body></html>


请再帮我看看,十分感谢!
回复

使用道具 举报

发表于 2015-3-20 23:13:09 | 显示全部楼层
站长苏飞 发表于 2015-3-20 08:20
先访问一下登录页面获取一个页面的Cookie然后带上这个Cookie再去Post就行了。

[mw_shl_code=csharp,true ...

飞哥,这个万能框架的请求好像出来点问题,被服务器按错误重定向,
回复

使用道具 举报

发表于 2015-3-20 23:14:43 | 显示全部楼层
本帖最后由 hell01234 于 2015-3-20 23:21 编辑

[C#] 纯文本查看 复制代码
        private string Stream2String(Stream st)
        {
            using (StreamReader sr = new StreamReader(st))
            {
                return sr.ReadToEnd();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {

            //第一次请求
            HttpClient hc1 = new HttpClient();
            HttpWebResponse rp1=    hc1.GetReponse("http://210.38.138.1:81/test.aspx", "get", null, false);
            string html1 = Stream2String(rp1.GetResponseStream());
            var cookie1 = rp1.Cookies;


            MatchCollection matchs = Regex.Matches(html1, "<input type=\"hidden\" name=\"(__\\w+)\".* value=\"(.*)\" />");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < matchs.Count; i++)
            {
                sb.Append(matchs.Groups[1]).Append("=").Append(matchs.Groups[2]).Append("&");
            }

            string postData = sb.ToString() + "txtlogintype=0&txtUsername_Lib=201211621426&txtPas_Lib=qwaszx&btnLogin_Lib=登录";
            postData = HttpUtility.UrlEncode(postData,Encoding.UTF8);
            //第二次请求
            CookieContainer cc1 = new CookieContainer();
            cc1.Add(cookie1);
            HttpClient hc2 = new HttpClient(cc1);
          HttpWebResponse rp2=  hc2.GetReponse("http://210.38.138.1:81/test.aspx","post",postData,false);
            var cookie2 = rp2.Cookies;
            //第三次请求
            cc1.Add(cookie2);
            HttpClient hc3= new HttpClient(cc1);
            HttpWebResponse rp3 = hc3.GetReponse("http://210.38.138.1:81/user/userinfo.aspx", "get", null, true);
            string htmlInfo = Stream2String(rp3.GetResponseStream());

        }
回复

使用道具 举报

发表于 2015-3-20 23:16:01 | 显示全部楼层
HttpClient类
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
namespace AutoLogin
{
    public class HttpClient
    {
        private CookieContainer cookieContainer = new CookieContainer();

        public HttpClient(CookieContainer cookies)
        {
            this.cookieContainer = cookies;
        }
        public HttpClient() { }

        public HttpWebResponse GetReponse(string url, string method, string data,bool autoRedirect)
        {
            NameValueCollection collection = new NameValueCollection();

            collection.Add("Accept-Encoding", "gzip,deflate,sdch");
            collection.Add("Accept-Language", "zh-CN,zh;q=0.8");
            Stream st = null;
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = method.ToUpper();
            req.CookieContainer = this.cookieContainer;
            req.Headers.Add(collection);
            req.AllowAutoRedirect = autoRedirect;
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";//用户代理
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("X-Requested-With", "XMLHttpRequest");

            if (method.ToLower() == "post" && !string.IsNullOrEmpty(data))
            {
                //Post提交数据方式
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] postBytes = encoding.GetBytes(data);
                req.ContentLength = postBytes.Length;

                st = req.GetRequestStream();
                st.Write(postBytes, 0, postBytes.Length);
                st.Close();
            }
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            return response;
        }


    }
}
回复

使用道具 举报

发表于 2015-3-20 23:19:47 | 显示全部楼层
我测试可以完美登陆,拿到登陆后的信息。用到system.web程序集来urlencodce@shunjiey  
回复

使用道具 举报

发表于 2015-3-20 23:23:00 | 显示全部楼层
我没找到万能框架请求那个不合理的地方在哪,你找到了的话跟我说下。
回复

使用道具 举报

 楼主| 发表于 2015-3-21 23:49:25 | 显示全部楼层
hell01234 发表于 2015-3-20 23:19
我测试可以完美登陆,拿到登陆后的信息。用到system.web程序集来urlencodce@shunjiey

谢谢大神!!!纠结了我好久的问题终于实现咯!!真的十分感谢!我会继续在这里学习的!还望指教!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-15 22:24

© 2014-2021

快速回复 返回顶部 返回列表