0

ASP.NET ページで WebClient を使用して Pinterest にログインしようとしています。これは私が現在使用している CookieWebAwareClient です。

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

    public CookieAwareWebClient(CookieContainer c)
    {
        this.CookieContainer = c;
        this.Headers.Add("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5");
    }

    public CookieContainer CookieContainer { get; set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
        }
        return request;
    }
}

ログインに使用しているコードは次のとおりです。最初に GET 要求を実行して、csrf トークンと使用する他の _ch 値を取得します。

var client = new CookieAwareWebClient();
client.Encoding = Encoding.UTF8;

string html = client.DownloadString("https://pinterest.com/login/");

string csrftoken = GetCSRFToken(html);
string chtoken = GetCHToken(html);

var values = new NameValueCollection();
values.Add("email", HttpUtility.UrlEncode(MY_EMAIL));
values.Add("password",MY_PASSWORD);
values.Add("csrfmiddlewaretoken", csrftoken);
values.Add("_ch", chtoken);

// 403 Error happens below
client.UploadValues("https://pinterest.com/login/?next=%2Flogin%2F", values);

html = client.DownloadString("http://pinterest.com/settings");

何か小さなものが欠けているように感じますが、それが何であるかを判断できません。

4

0 に答える 0