Myspace Login... Tricky headers!
Code:
TCPClient = New System.Net.Sockets.TcpClient("myspace.com", 80)
TCPClient.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf Read_Data, Nothing)
Data_To_Send = "POST http://login.myspace.com/index.cfm?fuseaction=login.process "
Data_To_Send = Data_To_Send & "&email=" & EmailTxt.Text & "&password=" & PasswordTxt.Text
sw = New IO.StreamWriter(TCPClient.GetStream)
sw.Write(Data_To_Send)
sw.Flush()
I'm tyring to create a program that logins into my myspace and retrieves if i have new comments messages etc etc. I'm using the Winsock namespace as you can tell.
When i send this data off to myspace i get:
Code:
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Thu, 10 Jan 2008 10:02:26 GMT
Connection: close
Content-Length: 20
<h1>Bad Request</h1>
Sent back to me. Now i know that this is because i'm not sending the correct headers and what not to the myspace server. I've tried catching the packets with a packet sniffer and HTTP Live Headers (With firefox) both returned gibberish. I've looked around for example codes for logging into myspace on these forums, but also, i get the Bad Request error returned to me.
So i'm hoping someone could help me out with finding out the correct things to send please.
Also, on another note:
Code:
RememberMeChk.Checked = GetSetting("Myspace Checker", "Settings", "Chk Remember Me", 1)
EmailTxt.Text = My.Settings.EmailText
Why does My.Settings.EmailText not save when the application is restarted? It will save when i try to read from it before i close the app, but it won't save once the app has been restarted. I also tried saving the text to the registry with the SaveSetting and GetSetting commands. It saves numbers, but they both don't save text. I find this very weird?
Re: Myspace Login... Tricky headers!
... Any suggestions for either problems?
Re: Myspace Login... Tricky headers!
Winsock Namespace?
I see mention of a socket, a tcpclient, a stream writer... an async call to begin reading. doesn't quite look like winsock to me.
anyways..
you should be using GET instead of POST
vb Code:
data_to_send = "GET /index.cfm?fuseaction=login.process"
data_to_send = data_to_send & "&email=" & EmailTxt.Text & "&password=" & PasswordTxt.Text & " "
data_to_send = data_to_send & "HTTP/1.1" & vbCrLf
data_to_send = data_to_send & "Host: login.myspace.com" & vbCrLf & vbCrLf
Re: Myspace Login... Tricky headers!
Oh really? I'll give it a shot, but i thought that GET was used when you wanted to parameters to be visible and POST when you don't.
TCPClient.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf Read_Data, Nothing)
I take it that that's the async call? Well the code that i used was copied and i didn't know what that line did, and since i'm new to .NET and using winsock this way i decided better just to leave it until later.
Quote:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/6.0
X-AspNet-Version: 2.0.50727
Set-Cookie: MSCulture=IP=220.253.87.47&IPCulture=en-AU&PreferredCulture=en-AU&Country=AU&ForcedExpiration=0&timeZone=0&USRLOC=QXJlYUNvZGU9MCZDaXR5PVRvb3dvb21iYSZDb3VudHJ5T29kZ T1BVSZDb3VudHJ5TFFtZT1BdXNFcmFsaWEmRG1hQ29kZT0XJkxhdGl0dWRlPS0ySy41NSZMb25naXR1ZGE9MTUxLjk2NjcmUG9zd GFeQ29kZT0mUmVnaWFITmFtZT0GNA==; domain=.myspace.com; expires=Thu, 17-Jan-2008 17:04:52 GMT; path=/
X-Server: b4579dc3bd6bff3d646bc5b5a65b9228705b10da8f778784
Set-Cookie: SessionDDF1=728312a9b8c19a01ba0ac752154cbc368e62be2f383c1963; domain=.myspace.com; path=/
Date: Thu, 10 Jan 2008 17:04:52 GMT
I Changed some numbers and letters in that quote incase it was possible to get access to my account... but what am i to do with that responce? At least it's better then the Bad Request responce... This one looks like a cookie.
Do i send the post message now? Let me experiment.
Ok...
Quote:
Data_To_Send = "POST /index.cfm?fuseaction=login.process"
Data_To_Send = Data_To_Send & "&email=" & EmailTxt.Text & "&password=" & PasswordTxt.Text & " "
Data_To_Send = Data_To_Send & "HTTP/1.1" & vbCrLf
Data_To_Send = Data_To_Send & "Content-Length: 175" & vbCrLf
Data_To_Send = Data_To_Send & "Host: login.myspace.com" & vbCrLf & vbCrLf
I've been stuffing around a bit and i found out that when i send this i get
HTTP/1.1 100 Continue
Sent to me. What's going on here. This is confusing, how does the browser know where to go from or... or how can i just get my main page up, lol.
Re: Myspace Login... Tricky headers!
Keep reading the socket, there's alot more data that comes down the line.
I suggest you do some research on how the HTTP protocol works.
Re: Myspace Login... Tricky headers!
Nothing more comes down the line...
I think i understand how the protocol works... Basically...
Do you have anything in the way that i'm looking for? Or can you help me with this code?
I didn't think it would be so complicated just to login and check your messages and that. There's a MSN Plus Plugin that i have that checks my myspace and when i go through that none of this is in there, just i don't know Java well enough to convert it into VB.
Code:
function Load_Notifications ()
{
if(LoggedIn == false){
xmlhttp=new ActiveXObject("MSXML2.ServerXMLHTTP");
xmlhttp.onreadystatechange=Check_Space;
xmlhttp.open("POST","http://login.myspace.com/index.cfm?fuseaction=login.process",true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("email="+user+"&password="+pass);
}
else GoGoGo();
}
function GoGoGo()
{
xmlhttp.open("GET", 'http://home.myspace.com/index.cfm?fuseaction=user', true);
xmlhttp.send(null);
}
That's all the code in the Myspace checker for MSN to actually read from myspace, the rest is phrasing the HTML and the GUI and that.
Re: Myspace Login... Tricky headers!
u could give this a wurl
vb Code:
Dim user As String = "test"
Dim pass As String = "test"
Dim uristring As String = "http://login.myspace.com/index.cfm?fuseaction=login.process"
Dim myWebClient As New WebClient()
Dim postData As String = "email=" + user + "&password=" + pass
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uristring)
Dim headers As New System.Net.WebHeaderCollection
headers.Add("Content-Type", "application/x-www-form-urlencoded")
myWebClient.Headers = headers
Dim responseArray As Byte() = myWebClient.UploadData(uristring, "POST", byteArray)
Console.WriteLine(System.Text.Encoding.Default.GetString(myWebClient.DownloadData("http://home.myspace.com/index.cfm?fuseaction=user")))
though i don't expect it to work, their login mechanism appear more complex
Re: Myspace Login... Tricky headers!
Code:
Imports System.Text
Dim Page_ As String
Dim user As String = EmailTxt.Text
Dim pass As String = PasswordTxt.Text
Dim uristring As String = "http://login.myspace.com/index.cfm?fuseaction=login.process"
Dim myWebClient As New System.Net.WebClient '
Dim postData As String = "email=" + user + "&password=" + pass
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uristring)
Dim headers As New System.Net.WebHeaderCollection
headers.Add("Content-Type", "application/x-www-form-urlencoded")
myWebClient.Headers = headers
Dim responseArray As Byte() = myWebClient.UploadData(uristring, "POST", byteArray)
Page_ = (System.Text.Encoding.Default.GetString(myWebClient.DownloadData("http://home.myspace.com/index.cfm?fuseaction=user")))
OK, this code does actually work, lol... but is it possible to do with the winsock? When this code above fires off the program freezes up until the process has finished. When using the winsock, the program works fine. There's no way to tell the user what's happening if the program is frozen and stuff like that. The headers are my main problem here =(, i think. I just need to know how to do it!
Re: Myspace Login... Tricky headers!
you know it would be a million times easier if you just used a browser control and automated the process of navigating to myspace.com, filling in the fields to login, and parsing the page after you login and looking for messages. You don't have to actually show the browser control on a form to be able to use it.
Looks like you already have a decent amount of work in the direction you are going in, but if you want to try another approach, check out the example in my sig for manipulating webpages with VB.
Re: Myspace Login... Tricky headers!
Yeah... i might end up doing this... but... why can't it work with winsock?
I'm going to redesign the program... but i still want to know why it wouldn't work with winsock.I'll have a look at your example and with the webclient control that TokersBall_CDXX talked about. I'll see which one works better. Though i think the webbrowser control will be better if it doesn't freeze the program.
Re: Myspace Login... Tricky headers!
Code:
WebB.Navigate("http://www.myspace.com")
sPage = WebB.Document.Body.InnerHtml
MsgBox(sPage)
When trying to read the page source i get this error:
"Object reference not set to an instance of an object."
I've tried other ways and i get the same error...
Re: Myspace Login... Tricky headers!
Code:
WebB.Navigate("http://www.vbforums.com")
sPage= WebBrowser.documentElement.innerHTML
Quote:
'documentElement' is not a member of 'System.Windows.Forms.WebBrowser'.
What is up with this? How come nothing works?
I also tried a mshtml way by referencing mshtml and a little bit of coding:
Code:
Dim hDoc As mshtml.HTMLDocument
hDoc = WebB.Document
But i get:
Quote:
Runtime errors might occur when converting 'System.Windows.Forms.HtmlDocument' to 'mshtml.HTMLDocument'.
And upon running the code i get this error:
"Object reference not set to an instance of an object."
Does anyone know what was wrong with the winsock way, or why this way is not working.
Everything i do is running into dead ends >=(.
Re: Myspace Login... Tricky headers!
1st thing, this won't work
Quote:
WebB.Navigate("http://www.myspace.com")
sPage = WebB.Document.Body.InnerHtml
MsgBox(sPage)
because you haven't even given the browser chance to complete it's navigation, you should retreive the WebB.Document.Body.InnerHtml from a document complete sub.
also you can't just convert a 'System.Windows.Forms.HtmlDocument' to a 'mshtml.HTMLDocument', maybe using DirectCast might work, but i can't coment on that.
you say what was wrong with the winsock way, do you mean winsock or System.Net.Sockets? because your original post used TcpClient which is not winsock, winsock is a vb6 ( & pre vb6 ) component.
you should be able to use sockets ( eg: a TcpClient ) , but may find that you have to sometimes set the AllowRedirect Headers to false, i find the best way is to use a HttpWebrequest, but sockets will work.
good luck.
Re: Myspace Login... Tricky headers!
you could place the webclient solution in a thread?
this isn't too difficult especially with newer versions of VB.net
Re: Myspace Login... Tricky headers!
Quote:
Originally Posted by TokersBall_CDXX
you could place the webclient solution in a thread?
this isn't too difficult especially with newer versions of VB.net
All of the methods of the webclient class have Async versions of themselves that do not block calling code.
Re: Myspace Login... Tricky headers!
Quote:
Originally Posted by dynamic_sysop
1st thing, this won't work
because you haven't even given the browser chance to complete it's navigation, you should retreive the WebB.Document.Body.InnerHtml from a document complete sub.
also you can't just convert a 'System.Windows.Forms.HtmlDocument' to a 'mshtml.HTMLDocument', maybe using DirectCast might work, but i can't coment on that.
you say what was wrong with the winsock way, do you mean winsock or System.Net.Sockets? because your original post used TcpClient which is not winsock, winsock is a vb6 ( & pre vb6 ) component.
you should be able to use sockets ( eg: a TcpClient ) , but may find that you have to sometimes set the AllowRedirect Headers to false, i find the best way is to use a HttpWebrequest, but sockets will work.
good luck.
Yeah, ok, I'll do that. The thing is that i pretty much got fed up and copied code completely from another project and pasted it in and just changed the URL and the same error happened, but the error wasn't in the other project =S.
Yep, i don't understand why it wouldn't work with the System.Net.Sockets way. Solving this problem would solve it with the old VB6 way, but sticking with the System.Net.Sockets would be most prudent.
Re: Myspace Login... Tricky headers!
Quote:
Originally Posted by kleinma
All of the methods of the webclient class have Async versions of themselves that do not block calling code.
Indeed they do, answered too quickly.
*jots down for future record*