使用 WebClient 或 WebRequest 登录网站并访问数据 [英] Using WebClient or WebRequest to login to a website and access data

查看:30
本文介绍了使用 WebClient 或 WebRequest 登录网站并访问数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 WebClient/WebRequest 访问网站上的受限数据.该网站上没有官方 API,所以我要做的只是填写 HTML 表单并将值发布到服务器,这样我就登录了.

I'm trying to access restricted data on a website using WebClient/WebRequest. There is no official API in that website, so what I'm trying to do is simply fill the HTML form and post the values to the server, so I'm logged in.

我尝试了这个this,但似乎未登录即将到来的请求.

I tried this and this, but it doesn't look like the upcoming requests are logged in.

后一个例子更吸引人,因为我显然更喜欢 WebClient,但传统的 WebRequest 也可以.

The latter example is much more appealing since I obviously prefer WebClient, but legacy WebRequest will do.

无论如何,在第一个示例中,我认为它确实登录了,但是即将到来的访问私有数据的请求会返回一个带有消息这是仅限会员的内容"的页面.

Anyway, in the first example I think it did login, but the upcoming requests that access the private data return a page with a message "This is member only content".

如何让 WebClient 永久登录?

推荐答案

更新:

参见 我的评论如下.

这是我所做的并且它有效(信用).

Here's what I did and it works (credit).

先添加这个类:

namespace System.Net
{
  using System.Collections.Specialized;
  using System.Linq;
  using System.Text;

  public class CookieAwareWebClient : WebClient
  {
    public void Login(string loginPageAddress, NameValueCollection loginData)
    {
      CookieContainer container;

      var request = (HttpWebRequest)WebRequest.Create(loginPageAddress);

      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";

      var query = string.Join("&", 
        loginData.Cast<string>().Select(key => $"{key}={loginData[key]}"));

      var buffer = Encoding.ASCII.GetBytes(query);
      request.ContentLength = buffer.Length;
      var requestStream = request.GetRequestStream();
      requestStream.Write(buffer, 0, buffer.Length);
      requestStream.Close();

      container = request.CookieContainer = new CookieContainer();

      var response = request.GetResponse();
      response.Close();
      CookieContainer = container;
    }

    public CookieAwareWebClient(CookieContainer container)
    {
      CookieContainer = container;
    }

    public CookieAwareWebClient()
      : this(new CookieContainer())
    { }

    public CookieContainer CookieContainer { get; private set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
      var request = (HttpWebRequest)base.GetWebRequest(address);
      request.CookieContainer = CookieContainer;
      return request;
    }
  }
}

用法:

public static void Main()
{
  var loginAddress = "www.mywebsite.com/login";
  var loginData = new NameValueCollection
    {
      { "username", "shimmy" },
      { "password", "mypassword" }
    };

  var client = new CookieAwareWebClient();
  client.Login(loginAddress, loginData);
}

这篇关于使用 WebClient 或 WebRequest 登录网站并访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆