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

标签 c# asp.net web-services httpwebrequest

<分区>

假设我正在构建一个 C# 应用程序。 申请目的:

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

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

  1. 使用这些凭据登录网站。
  2. 然后点击登录后出现的 anchor 链接。
  3. 找出保存信息的跨度。
  4. 获取信息。

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

有没有类似目的的教程/信息/文章?我只是不知道要搜索什么?

最佳答案

HttpWebRequests 基本介绍

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

通过导航至工具 > 实时 HTTP header 加载实时 HTTP header 。一旦你加载了 GUI 导航到你想登录的网站,我将使用 Facebook用于演示目的。输入准备好登录的凭据,但在您这样做之前清除 GUI 文本窗口并确保标记为 Capture 的复选框已选中。一旦你点击登录,你会看到文本窗口充满了关于请求的各种信息,包括你需要的 POST 数据。

我发现最好单击全部保存...,然后在文本文档中搜索您的用户名,这样您就可以轻松识别 POST 数据。对于我的请求,POST 数据如下所示:

lsd=AVp-UAbD&display=&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&timezone=0&lgnrnd=214119_mDgc&lgnjs=1356154880&email=%myfacebookemail40outlook.com&pass=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");

我的目的是向您展示我们可以通过 Web 浏览器“手动”发送的 POST 数据与我们如何使用所述数据在 C# 中模拟请求之间的关系。了解发送 POST 数据远非确定性。不同的网站以不同的方式工作,可以按照您的方式提供各种东西。下面是我用来验证 Facebook 凭据是否正确的函数。我不能也不应该在这里过于深入,因为这些类及其成员都有很好的 self 记录。关于在 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");
    }

关于c# - 如何在没有浏览器的情况下与网站互动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000185/

相关文章:

c# - 从 C# 代码重新加载 ASP.NET 站点中的 iframe

c# - WebAPI POST 方法抛出空异常

c# - .Net 4.0 框架和 4.5 框架之间的区别?

asp.net - 如何在 C# 中将视频从 H.264 转换为 H.265

web-services - 使用 CXF 2.5.2 和 JBoss AS 7 的问题

java - 用Java读取XSD文件

c# - wpf:无法识别左键单击

c# - 如何以编程方式将事件接收器附加到 SPList?

c# - ASP.Net C# GridView

c# - 从 Web 应用程序中检索原始用户身份