如何在没有浏览器的情况下与网站互动?

Bil*_*ani 2 c# asp.net web-services httpwebrequest

说我正在构建ac#应用程序.申请目的:

  1. 从用户获取用户名和密码.
  2. 并显示网站上的一些信息.

在后台,取用用户名和密码后,它应该:

  1. 使用这些凭据登录网站.
  2. 并单击登录后显示的锚链接.
  3. 找出保存信息的范围.
  4. 得到信息.

这是一个例子.我实际上正在构建一个应用程序来显示带宽使用信息.服务器不会为此公开任何API.

是否有任何教程/信息/文章可用于类似目的?我只是不寻找什么?

Cas*_*roy 15

HttpWebRequests的基本介绍

首先,您将需要适合该工作的工具.去下载适用于Firefox 的Live HTTP Headers插件.这将允许您实时查看HTTP标头,以便您可以查看与网站交互时发送的POST数据.一旦您知道发送到网站的数据,您就可以通过编程方式创建自己的HTTP Web请求来模拟该过程.工具>实时HTTP标头

通过导航到" 工具">"实时HTTP标头"来加载实时HTTP标头.一旦你加载了GUI导航到你想登录的网站,我将使用Facebook进行演示.输入您已准备好登录的凭据,但在此之前清除GUI文本窗口并确保选中标记为Capture的复选框.登录后,您将看到文本窗口中包含有关请求的各种信息,包括您需要的POST数据.

我发现最好单击全部保存...然后在文本文档中搜索您的用户名,以便您可以轻松识别POST数据.对于我的请求,POST数据看起来像这样:

LSD = AVP-UAbD&显示=&legacy_return = 1&return_session = 0&trynum = 1&charset_test =%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C %D0%94%2C%D0%84&时区= 0&lgnrnd = 214119_mDgc&lgnjs = 1356154880&电子邮件=%myfacebookemail40outlook.com&传= myfacebookpassword&default_persistent = 0

然后可以在C#中定义,如下所示:

StringBuilder postData = new StringBuilder();
postData.Append("lsd=AVqRGVie&display=");
postData.Append("&legacy_return=1");
postData.Append("&return_session=0");
postData.Append("&trynum=1");
postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84");
postData.Append("&timezone=0");
postData.Append("&lgnrnd=153743_eO6D");
postData.Append("&lgnjs=1355614667");
postData.Append(String.Format("&email={0}", "CUSTOM_EMAIL"));
postData.Append(String.Format("&pass={0}", "CUSTOM_PASSWORD"));
postData.Append("&default_persistent=0");
Run Code Online (Sandbox Code Playgroud)

我的目标是向您展示我们可以通过Web浏览器"手动"发送的POST数据与我们如何使用所述数据模拟C#中的请求之间的关系.了解发送POST数据远非确定性.不同的网站以不同的方式工作,可以按照您的方式抛出各种各样的东西.以下是我汇总的功能,以验证Facebook凭据是否正确.我不能也不应该在这里深入研究,因为班级及其成员都有很好的自我记录.您可以找到比我在MSDN上使用的方法更好的信息,例如WebRequest.Method Property

    private bool ValidateFacebookCredentials(string email, string password)
    {
        CookieContainer cookies = new CookieContainer();
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        string returnData = string.Empty;

        //Need to retrieve cookies first
        request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1"));
        request.Method = "GET";
        request.CookieContainer = cookies;
        response = (HttpWebResponse)request.GetResponse();

        //Set up the request
        request = (HttpWebRequest)WebRequest.Create(new Uri("https://www.facebook.com/login.php?login_attempt=1"));
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13";
        request.Referer = "https://www.facebook.com/login.php?login_attempt=1";
        request.AllowAutoRedirect = true;
        request.KeepAlive = true;
        request.CookieContainer = cookies;

        //Format the POST data
        StringBuilder postData = new StringBuilder();
        postData.Append("lsd=AVqRGVie&display=");
        postData.Append("&legacy_return=1");
        postData.Append("&return_session=0");
        postData.Append("&trynum=1");
        postData.Append("&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84");
        postData.Append("&timezone=0");
        postData.Append("&lgnrnd=153743_eO6D");
        postData.Append("&lgnjs=1355614667");
        postData.Append(String.Format("&email={0}", email));
        postData.Append(String.Format("&pass={0}", password));
        postData.Append("&default_persistent=0");

        //write the POST data to the stream
        using(StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            writer.Write(postData.ToString());

        response = (HttpWebResponse)request.GetResponse();

        //Read the web page (HTML) that we retrieve after sending the request
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            returnData = reader.ReadToEnd();

        return !returnData.Contains("Please re-enter your password");
    }
Run Code Online (Sandbox Code Playgroud)

  • 登录成功后使用此代码.可以进入浏览器并在Facebook中导航而无需再次在浏览器中输入凭据吗?我的意思是,用户会话是否保持活动状态,就像我们直接通过浏览器登录一样? (2认同)