C# 采集网页,可能需先登陆

采集网址:https://www.taoguba.com.cn/moreTopic?userID=252069

需给出demo,测试通过后马上采纳
哪位能帮忙看看呀

c#
wiledog
wiledog 回复caozhy: 谢谢
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复wiledog: 稍等
11 个月之前 回复
wiledog
wiledog 谁能帮忙看看呀
11 个月之前 回复
wiledog
wiledog 回复wiledog: caozhy在不在
11 个月之前 回复
wiledog
wiledog 我的:zhaojing1840@163.com,谁能解决我发送登陆账号
11 个月之前 回复

4个回答

思路就是抓包,然后分析有什么cookie带上,你也可以自己试试看。
你的用户名密码是什么,我给你写了邮件,你回一下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace Q750623
{
    class Program
    {
        public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = encoding.GetBytes(postData);
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            myRequest.CookieContainer = cookie;
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }

        public static string GetRequest(string requestUrlString, CookieContainer cookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrlString);

            request.Method = "GET";
            request.CookieContainer = cookie;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }

        public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
        {
            byte[] data = Encoding.UTF8.GetBytes(postData);
            //向服务端请求
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;
            myRequest.CookieContainer = new CookieContainer();
            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            //将请求的结果发送给客户端(界面、应用)
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            cookie.Add(myResponse.Cookies);
            StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }

        static void Main(string[] args)
        {
            CookieContainer cookie = new CookieContainer();
            PostRequest("userName=用户名&password=密码&checkCode=&save=Y&url=https%3A%2F%2Fwww.taoguba.com.cn%2FmoreTopic%3FuserID%3D252069", "https://sso.taoguba.com.cn/web/login/submit", cookie);
            string s = GetRequest("https://www.taoguba.com.cn/moreTopic?userID=252069", cookie);
            Console.WriteLine(s);
        }
    }
}

wiledog
wiledog 不好意思忙别的,刚看到,马上试试
10 个月之前 回复
wiledog
wiledog C#大牛来csdn帮忙看看啊:)
11 个月之前 回复
wiledog
wiledog 已发送邮件,感谢
11 个月之前 回复

这个 很容易啊 找到 用户名框 获取焦点 sendkeys 发送 用户名 然后 在 发送 {TAB} 到密码框 senkeys 密码 如果sendkeys没用 就用 winring0 (这个一般用来输入银行的安全控件密码) 在然后 发送 3次 {TAB} 或者 找到 login 的按钮 元素 click 或者 {ENTER} 解决

wiledog
wiledog 抓包没有看到post方法,手动登陆的cookie能直接用吗?有空的话帮忙写个demo吧,谢谢
11 个月之前 回复
wiledog
wiledog 谢谢 我先看看
11 个月之前 回复

System.Net.CookieContainer cc = new System.Net.CookieContainer();
string t = @"password=&save=Y&url=https://www.taoguba.com.cn/index?blockID=1&userName=";
oGatherWeb.HttpPost(@"https://www.taoguba.com.cn/index?blockID=1",t, ref cc);
oGatherWeb.HttpGet(@"https://www.taoguba.com.cn/blog/2790539", null, cc);
public string HttpPost(string Url, string postDataStr, ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

        byte[] postData = Encoding.UTF8.GetBytes(postDataStr);
        request.ContentLength = postData.Length;
        request.CookieContainer = cookie;
        Stream myRequestStream = request.GetRequestStream();
        myRequestStream.Write(postData, 0, postData.Length);
        myRequestStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Cookies = cookie.GetCookies(response.ResponseUri);
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();

        return retString;
    }
    public string HttpGet(string Url, string postDataStr, CookieContainer cookie)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
        request.Method = "GET";
        request.ContentType = "text/html;charset=UTF-8";
        request.CookieContainer = cookie;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();

        return retString;
    }
wiledog
wiledog 这个方法不行
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问