Re: Help trying to login to myspace programatically in vb.net



I must say, Nick, that's some pretty frickin' amazing pseudocode. :-)
Robin S.
-------------------------------------
"Nick Malik [Microsoft]" <nickmalik@xxxxxxxxxxxxxxxxxx> wrote in message
news:roydnUYMn4QQXAfYnZ2dnUVZ_ompnZ2d@xxxxxxxxxxxxxx
How about I give you psuedo code? I'm at home on vacation at the
moment, and the hour it would take me to "whip up login code" is more
than I want to take away from my kids at the moment.

The idea is that a web browser is just a computer program. You will
write a computer program that pretends to be a web browser.
Therefore, your program has to behave like a web browser. We don't
need a browser control, and we don't need to download images or lay
out the page, but we do need to download the HTML page itself, and all
associated cookies, and we need to send those cookies back to the site
on every subsequent call.

I stole some of this code from dotnetspider.com

using system.net;
class myspace
{

// declare a variable where you will store the cookies that
myspace drops on your 'browser'
cookiecontainer myspacecookies = new cookiecontainer(); // I
don't remember if this is the correct way to initialize the cookie
list.
// kludge: you should get the login page from a config file. For
this example, I will hardcode it, but this is not right
String loginpage = "http://login.myspace.com";;
// this may be wrong. I have not examined the HTML to see what
the actual post page is
String postpage = "http://login.myspace.com";;

public sub login(String username, String password)
{

System.Net.WebResponse response = null;

try
{
// Setup our Web request
WebRequest request = WebRequest.Create(loginpage);
request.method= "GET";
request.Timeout = timeoutSeconds * 1000;

// Retrieve data from request
response = request.GetResponse();
// if you want your code to be really reliable,
inspect the response. Make sure it looks like you expect it to look.
// it should be the form HTML for the login page.
myspacecookies = response.cookiecontainer;
response.close();

request = WebRequest.Create(postpage);
request.method = "POST";
request.cookiescontainer = myspacecookies; //
likely syntax error here... look up cookie containers

// I did not examine the HTML for the login page,
so I do not know the names of the variables that need to be sent
// back in the post. If these are not the correct
names, then this code won't work. Look up the names in the HTML of
// the login page. Suggestion: write the variable
names into your config file, in case myspace decides to change their
// login page, it won't be hard to change your app
to match.
string postData="username=" + username +
"&password=" + password;
ASCIIEncoding encoding=new ASCIIEncoding();
byte[] byte1=encoding.GetBytes(postData);
// Set the content type of the data being posted.

request.ContentType="application/x-www-form-urlencoded";
// Set the content length of the string being
posted.
request.ContentLength=postData.Length;

response = request.GetResponse();
// you should be logged in now. THis response is
probably a redirect to the correct home page.
// you need to capture the cookies at this point
and use them in ALL subsequent requests to the site
myspacecookies = response.cookiecontainer;


}
catch (Exception ex)
{
// Error occured grabbing data, return empty string.
MessageBox.Show(this, "An error occurred while
retrived the HTML content. " + ex.Message,
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
// Check if exists, then close the response.
if ( response != null )
{
response.Close();
}
}


}

}

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"AppleBag" <applebag@xxxxxxxxx> wrote in message
news:1167604148.199494.115920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thank you for replying but that is exactly what i meant in my first
post.

I have been trying for weeks to get it to work on my own and just end
up scrapping it. So I really don't have any useful code to paste
since
it may not even be close to something workable. I'm still very new to
.net.

I was hoping someone who is familiar with creating code to login to
pages would come across this post and it would be easy to just whip
it
up themself and paste the code for me to study and (of course use and
build on).

Please if you (or someone else) don't mind, just goto
login.myspace.com
and see if you can whip up some working login code? I'd much rather
prefer a method without using a webbrowser control if possible
because
I believe it would be faster to directly send the form posts than to
wait for a slow web browser control to first load pages before
sending
them, etc.

Thanks!

Nick Malik [Microsoft] wrote:
If you have tried it, then you must have code. Please post a short
but
working example of your code that allows us to (a) see what you are
doing,
(b) look for mistakes, and (c) allows us to write a demo bit of code
more
quickly.

If you have not tried it, then try it first. I suggest that you
make a web
request against the login page, get the cookies, post a response
using the
variables that were provided on the login form along with the
cookies, get
the response, complete with new cookies, and then post the redirect
to the
home page.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"AppleBag" <applebag@xxxxxxxxx> wrote in message
news:1167356420.923702.255920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I'm having the worst time trying to login to myspace through code.
Can
someone tell me how to do this? Please try it yourself before
replying,
only because I have asked this a couple of times in the past in
other
places, and while the help was much appreciated, it seemed
everyone
just wanted to 'theoretically' explain how to do it, but when I
tried
to do it myself, I couldn't login.

I want to simply pass the email address and password to the login
form
and have it return the source code to me so that I can also
retrieve
the source code to other myspace pages as my logged in user.

Ultimately I'd like to make a simple vb.net login class to doit.

Huge thanks in advance for anyone who helps!






.