views:

73

answers:

1

I have tried so many ways but failed to logged in purevolume.com programatically!!!

login URL : http://www.purevolume.com/login

can anybody give it a try and provide sample code to achieve this.

+1  A: 

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();
    }
}
Jesper Palm
i tried this but page still remains on the login page and statusCode is true...how could i know that provided username and password is correct ? i think response should return the stream of "http://www.purevolume.com/dashboard" (page which opens after successfull login)...since i am a new bie so i am expecting perfect running code :(
xtremist
@xtremist: I don't have an account so it's hard for me to see what the problem is. But you should really use a tool like Fiddler or similar. With Fiddler you can see the raw HTTP requests and you should use this to find the difference when logging in with a normal browser vs the above code.
Jesper Palm
@xtremist: what does the Debug.WriteLine output? The response text is outputted there.
Jesper Palm
@xtremist: See my updated answer.
Jesper Palm
@Jesper Palm, Sir you are great !!!! really appreciate your working from the core of my heart ...
xtremist