使用multipart / form-data登录网站

时间:2013-11-25 00:39:52

标签: c# forms post httpwebrequest webrequest

我已经尝试了一段时间,使用WebRequest登录网站但没有运气,我希望有人可以帮助我。登录页面使用“multipart / form-data; boundary”作为我之前从未遇到过的帖子数据,但我无法让它工作。

成功的帖子看起来像这样(Tamper提供):

-----------------------------13327156328034
Content-Disposition: form-data; name="cc_session_id"

0vtgfe4bbhlh94f4vmmptctr06
-----------------------------13327156328034
Content-Disposition: form-data; name="cc_action"

cca_login
-----------------------------13327156328034
Content-Disposition: form-data; name="cc_failure_url"

https://www.fanduel.com/p/LoginPp
-----------------------------13327156328034
Content-Disposition: form-data; name="cc_success_url"

https://www.fanduel.com/
-----------------------------13327156328034
Content-Disposition: form-data; name="email"

*********
-----------------------------13327156328034
Content-Disposition: form-data; name="password"

*********
-----------------------------13327156328034
Content-Disposition: form-data; name="login"

Log in
-----------------------------13327156328034--

我的连接方法如下所示:

    private static WebResponse Connect()
    {
        var manager = new SessionIDManager();
        var boundary = "---------------------------" + DateTime.Now.Ticks;
        var newLine = Environment.NewLine;
        var propFormat = "--" + boundary + newLine +
                            "Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine +
                            "{1}" + newLine;

        var req = WebRequest.Create("https://www.fanduel.com/c/CCAuth");
        var session = manager.CreateSessionID(HttpContext.Current);
        req.Method = "POST";
        req.ContentType = "multipart/form-data; boundary=" + boundary;
        string formParams = string.Format(propFormat, "cc_session_id", session); 
        formParams += string.Format(propFormat, "cc_action", "cca_login");
        formParams += string.Format(propFormat, "cc_failure_url", "https://www.fanduel.com/p/LoginPp");
        formParams += string.Format(propFormat, "cc_success_url", "https://www.fanduel.com/");
        formParams += string.Format(propFormat, "email", Credentials.ToInsecureString(Credentials.DecryptString(Settings.FanDuel.UserName)));
        formParams += string.Format(propFormat, "password", Credentials.ToInsecureString(Credentials.DecryptString(Settings.FanDuel.Password)));
        formParams += string.Format(propFormat, "login", "Log in");
        formParams += "--" + boundary + "--";

        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }

        return req.GetResponse();
    }

它产生以下帖子数据:

-----------------------------635209175732301763
Content-Disposition: form-data; name="cc_session_id"

lptqmgshh2givmblbna4yeql
-----------------------------635209175732301763
Content-Disposition: form-data; name="cc_action"

cca_login
-----------------------------635209175732301763
Content-Disposition: form-data; name="cc_failure_url"

https://www.fanduel.com/p/LoginPp
-----------------------------635209175732301763
Content-Disposition: form-data; name="cc_success_url"

https://www.fanduel.com/
-----------------------------635209175732301763
Content-Disposition: form-data; name="email"

********
-----------------------------635209175732301763
Content-Disposition: form-data; name="password"

********
-----------------------------635209175732301763
Content-Disposition: form-data; name="login"

Log in
-----------------------------635209175732301763--

不幸的是,响应的cookie不是成功登录的cookie。在这一点上,我不知道还能做什么,我将不胜感激任何人愿意为我提供帮助。感谢。

更新:我尝试获得初始回复,然后将该会话ID用于我的帖子,但仍然没有运气。有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

我已经设法让这个工作并尽我所能优化了方法。

private CookieContainer cookies = new CookieContainer();
private string loginCookie;

public bool IsLoggedIn { get; set; }

public void Login()
{
    var boundary = "---------------------------" + DateTime.Now.Ticks;
    var newLine = Environment.NewLine;
    var propFormat = "--" + boundary + newLine +
        "Content-Disposition: form-data; name=\"{0}\"" +
        newLine + newLine + "{1}" + newLine;

    var req = WebRequest.Create("https://www.fanduel.com/c/CCAuth") as HttpWebRequest;
    req.CookieContainer = cookies;
    req.Method = "POST";
    req.ContentType = "multipart/form-data; boundary=" + boundary;
    string formParams = string.Format(propFormat, "cc_session_id", new SessionIDManager().CreateSessionID(HttpContext.Current));
    formParams += string.Format(propFormat, "cc_action", "cca_login");
    formParams += string.Format(propFormat, "cc_failure_url", "https://www.fanduel.com/p/LoginPp");
    formParams += string.Format(propFormat, "cc_success_url", "https://www.fanduel.com/");
    formParams += string.Format(propFormat, "email", Credentials.ToInsecureString(Credentials.DecryptString(FanDuel.Default.UserName)));
    formParams += string.Format(propFormat, "password", Credentials.ToInsecureString(Credentials.DecryptString(FanDuel.Default.Password)));
    formParams += string.Format(propFormat, "login", "Log in");
    formParams += "--" + boundary + "--";

    byte[] bytes = Encoding.ASCII.GetBytes(formParams);
    req.ContentLength = bytes.Length;
    using (Stream os = req.GetRequestStream())
    {
        os.Write(bytes, 0, bytes.Length);
    }

    var res = req.GetResponse();
    res.Close();

    loginCookie = req.Headers["Cookie"];

    IsLoggedIn = res.ResponseUri == new Uri("https://www.fanduel.com/p/Home");
}

然后将loginCookie传递给任何其他相关的WebRequest

void SomeMethod()
{
    var req = WebRequest.Create("SomeUri");
    req.Headers.Add("Cookie", loginCookie);
    var res = req.GetResponse();
}

我希望将来可以帮助某人。