Help trying to login to myspace programatically in vb.net

A

AppleBag

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!
 
N

Nick Malik [Microsoft]

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.
 
A

AppleBag

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!
 
N

Nick Malik [Microsoft]

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.
 
R

RobinS

I must say, Nick, that's some pretty frickin' amazing pseudocode. :)
Robin S.
-------------------------------------
Nick Malik said:
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 said:
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!
 
B

Bonney Armstrong

As there are no original ideas for books or movies, there are also very
few computing needs that haven't been addressed either. To wit: If
you're using Firefox, there are tons of extensions to the browser that
already do this for you. I am enclosing a partial list, without
preference for any:
https://addons.mozilla.org/firefox/3292/
https://addons.mozilla.org/firefox/2895/
https://addons.mozilla.org/firefox/3229/
https://addons.mozilla.org/firefox/3191/
https://addons.mozilla.org/firefox/899/

All of these sign you into myspace.com once you configure the tool,
plus perform various other functions. If you're curious as to *how*
it's done, then just look at their code and see how they're doing it.
I'm very monkey-see monkey-do that way, and it really helps me code
better when I see how other people have done certain things. You'll
save time by starting where they left off, too.
 
A

AppleBag

Hello Nick,

Thank you very much for the code. I have been playing with it and ran
into a wall that I was hoping you could help me over.

I am not sure if I am using the cookie container right, but it 'seems'
to a newbie like me that it should work. But I am probably wrong. Also,
on the second page request, the app is just hanging forever instead of
returning any data.

Here is the current code that I am using which is your code translated
to VB.NET plus some of my work (the form in design view just has a
button, and 2 txt boxes for the login email and password):

Public Class Form1

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

Public Sub login(ByVal strEmail As String, ByVal strPassword As
String)

Dim response As System.Net.WebResponse = Nothing

Try

' Setup our Web request
Dim request As Net.HttpWebRequest =
CType(Net.WebRequest.Create(loginpage), Net.HttpWebRequest)
Dim timeoutSeconds As Integer = 30

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.
'Dim instance As
Web.Services.Protocols.HttpWebClientProtocol = Nothing
Dim myspacecookies As New Net.CookieContainer

'myspacecookies = instance.CookieContainer
myspacecookies = request.CookieContainer ' <--- ## Cookies
are empty?
response.Close()

request = CType(Net.WebRequest.Create(postpage),
Net.HttpWebRequest)
request.Method = "POST"
' Likely syntax error here... look up cookie containers
request.CookieContainer = myspacecookies

' 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.
Dim postData As String = "email=" + strEmail + "&password="
+ strPassword
Dim encoding As New System.Text.ASCIIEncoding
Dim byte1() As Byte = 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() ' <--- ## Hangs here
' 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 = request.CookieContainer

Catch ex As Exception

' Error occured grabbing data, return empty string.
MessageBox.Show("An error occurred while retrieving the
HTML content. " + ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error)

Finally

' Check if exists, then close the response.
If Not response Is Nothing Then
response.Close()
End If

End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

login(txtEmail.Text, txtPass.Text)

End Sub

End Class

One last question, how come on the first click of the Button to do a
login using the webrequest object, does it seem to always take so long
to fetch the code from the web? I have noticed this in other apps using
it as well.

Thanks a million again for any help.

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 said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top