logged in to PureVolume.com programmatically

可紊 提交于 2019-12-08 13:34:33

Something like this should work... You need to do an http post request and send your username and password. To remain "logged in" you need a cookie container that you then reuse for all other requests.

Edit: Updated code that is tested. An initial GET request to /login was needed to have the session cookie before doing the POST login. I've also added some HTTP headers to make it identical to a "normal" request from a browser.

void Main()
{
    //We need a container to store the cookies in.
    CookieContainer cookies = new CookieContainer();

    //Request login page to get a session cookie
    GETHtml("http://www.purevolume.com/login", cookies);

    //Now we can do login
    Login("some-user-name", "some-password", cookies);
}

public bool Login(string Username, string Password, CookieContainer cookies)
{
    string poststring = string.Format("username={0}&password={1}&user_login_button.x=63&user_login_button.y=13&user_login_button=login",
                                Username, Password);
    byte[] postdata = Encoding.UTF8.GetBytes(poststring);

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.purevolume.com/login");
    webRequest.CookieContainer = cookies;
    webRequest.Method = "POST";
    webRequest.Referer = "http://www.purevolume.com/login";
    webRequest.Headers.Add("origin", "http://www.purevolume.com");
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = postdata.Length;
    using (Stream writer = webRequest.GetRequestStream())
        writer.Write(postdata, 0, postdata.Length);

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        //We need to add any response cookies to our cookie container
        cookies.Add(webResponse.Cookies);

        //Only for debug
        using (var stream = new StreamReader(webResponse.GetResponseStream()))
            System.Diagnostics.Debug.WriteLine(stream.ReadToEnd());

        return (webResponse.StatusCode == HttpStatusCode.OK);
    }
}

public string GETHtml(string url, CookieContainer cookies)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.CookieContainer = cookies;
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022;";
    webRequest.Referer = "http://www.purevolume.com/login";

    using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
    {       
        //We need to add any response cookies to our cookie container           
        cookies.Add(webResponse.Cookies);

        using (var stream = new StreamReader(webResponse.GetResponseStream()))
            return stream.ReadToEnd();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!