Hack Forums
[.NET] Tagged Login Example - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Tagged Login Example (/showthread.php?tid=4454240)



Tagged Login Example - Cairns - Sep 20, 2014

Someone asked for an example on how to log into m.tagged.com with a httpwebrequest so I quickly written an example. Not all responses have been convered since this is only an example.

Pastebin:
Code
http://pastebin.com/4t6PRaSn

Usage:

Declare the class, assign the properties and call 2 methods.

Code
Dim API As New TaggedAPI
With API
     .Username = ""
     .Password = ""
End With

Try
     Dim Result As TaggedAPI.Result = API.GetFormToken()
     Debug.Print(Result.Message)
     If Result.Success Then
      Result = API.Login()
      Debug.Print(Result.Message)
     End If
Catch ex As Exception
     MessageBox.Show(ex.Message)
End Try



* - Cornflower Blue இڿڰۣ-ڰۣ— - Sep 20, 2014

Why are you using "Select Case True"?


* - Cairns - Sep 20, 2014

(Sep 20, 2014 03:51 AM)Cornflower Blue Wrote:  Why are you using "Select Case True"?

I knew someone would bring that up.

I always use if and elseif, however I wanted to play with select case true because I seen a thread about it. It's just out of experimentation really, I will always stick with if and elseif though.

Code
http://c2.com/cgi/wiki?VbFlexibleSelectCase
This article talks about it a bit.

There is not a problem with using either in my opinion depending on the purpose. Let me know what you think


* - TheBarret - Sep 20, 2014

(Sep 20, 2014 04:26 AM)Cairns Wrote:  I knew someone would bring that up.

I always use if and elseif, however I wanted to play with select case true because I seen a thread about it. It's just out of experimentation really, I will always stick with if and elseif though.

Code
http://c2.com/cgi/wiki?VbFlexibleSelectCase
This article talks about it a bit.

There is not a problem with using either in my opinion depending on the purpose. Let me know what you think

I find that quit confusing actually, but i see the use for it with the default value, but "Case Else" could do the same , sort of i suppose.

This maybe a weird and offtop but in my script engine i have an additional 'Option" for Switch Case.

Code
Switch <expr> option <int> {
  Case <expr> { <statements> }
  Case <expr> { <statements> }
  Case <expr> { <statements> }
}

With this param i can change the behavior of my switch case, by default the select case has only one
possible case it takes, with the option 2, this is not the case, it will evaluate all cases and execute those
that have a positive condition, and executes them in that same order.

anyway, thats my 2 cents ^^


* - NETExplorer - Sep 20, 2014

This is my response. Take it or leave it
Code
Public Sub ResetCookies()
      Cookies = Nothing
      Cookies = New CookieContainer
End Sub
Can be shortened to this, you don't need to set it to nothing
Code
Public Sub ResetCookies()
      Cookies = New CookieContainer
End Sub

Instead of doing .Close() and .Dispose() with your streams, throw them in a using statement

Code
Dim StreamData As String = String.Empty
Do Until Stream.EndOfStream
    StreamData = Stream.ReadLine
    Data.Append(StreamData)
Loop
There are a couple of things that are redundant with this
1) Why are you setting StreamData to Stream.ReadLine, and then appending StreamData? Why not go straight with ReadLine?
2) Why are you even looping through the stream, use StreamReader.ReadToEnd()
Here is the fix:
Code
Dim StreamData As String = Stream.ReadToEnd()

There are a couple of more things in your code that I personally wouldn't do, but there isn't really a problem with them.

One last thing, why let the user control the retrieval of the token?


* - TheBarret - Sep 20, 2014

(Sep 20, 2014 08:18 AM)PatPositron Wrote:  This is my response. Take it or leave it
Code
Public Sub ResetCookies()
      Cookies = Nothing
      Cookies = New CookieContainer
End Sub
Can be shortened to this, you don't need to set it to nothing
Code
Public Sub ResetCookies()
      Cookies = New CookieContainer
End Sub

Instead of doing .Close() and .Dispose() with your streams, throw them in a using statement

Code
Dim StreamData As String = String.Empty
Do Until Stream.EndOfStream
    StreamData = Stream.ReadLine
    Data.Append(StreamData)
Loop
There are a couple of things that are redundant with this
1) Why are you setting StreamData to Stream.ReadLine, and then appending StreamData? Why not go straight with ReadLine?
2) Why are you even looping through the stream, use StreamReader.ReadToEnd()
Here is the fix:
Code
Dim StreamData As String = Stream.ReadToEnd()

There are a couple of more things in your code that I personally wouldn't do, but there isn't really a problem with them.

One last thing, why let the user control the retrieval of the token?

This, it's exactly how i would put in my mind, nicely said Pat, couldn't said any better myself. ✌


* - Cairns - Sep 20, 2014

I use using all the time so it automatically disposes a object that implements idisposable, so it's not like I don't know about it. I just kinda put it there for the person who requested it so there was no other confusion for him. And to my knowledge "using" does not "close" a stream automatically, it only disposes it. So even when I do use "using" I still close a stream before it's disposed. Unless microsoft have it automatically close the stream when the dispose method is called.

I am using a loop for getting the stream data because trying to load an entire page of content at once is silly.

As for the readline, I have appended it to a stringbuilder before without storing it into another variable and weirdly it was missing out data (don't ask why, not sure) so that's why I stored it in another variable weirdly resolved my fix months back, that's why I still do that.

I couldn't be bothered making the login fully automatic, the person only asked for an example to log into the account via a httpwebrequest, not a complete class for it otherwise I would have made it call the token method before logging in automatically.

It's just a rough quick mockup for him to see how it works really, I did not intend to create a good source for it.

Thanks for the comments, however I don't require them because I won't be updating it etc.


* - Dialog - Sep 20, 2014

(Sep 20, 2014 10:35 AM)Cairns Wrote:  And to my knowledge "using" does not "close" a stream automatically, it only disposes it. So even when I do use "using" I still close a stream before it's disposed. Unless microsoft have it automatically close the stream when the dispose method is called.
http://referencesource.microsoft.com/#mscorlib/system/io/streamreader.cs

[Image: GoM5T2V.png]

"stream.Close();"


* - Sn0wX - Sep 20, 2014

more code from users who dont understand what they r posting


* - NETExplorer - Sep 20, 2014

(Sep 20, 2014 03:16 PM)Sn0wX Wrote:  more code from users who dont understand what they r posting

I have a complete understanding of what I am posting, thank you.


* - SIM-1676753 - Sep 20, 2014

(Sep 20, 2014 03:48 PM)PatPositron Wrote:  I have a complete understanding of what I am posting, thank you.

Shhh Pat. SnowX is right and you are wrong. 😠


* - Walt.Jun10r - Sep 24, 2014

Hey, Thanks very much for this tutorial. I have wanted to make a program which gets a token for a while.

Anychance you got skype, I may need a little help ? I have been working on it for over 2 days and cant figure it out.


* - Cairns - Sep 24, 2014

If you want to get a token from the tagged log-in page, the source I provided will show you how to do that. If you're developing something completely different then I cannot help sadly, I don't have enough free time.


* - Walt.Jun10r - Sep 24, 2014

(Sep 24, 2014 09:12 AM)Cairns Wrote:  If you want to get a token from the tagged log-in page, the source I provided will show you how to do that. If you're developing something completely different then I cannot help sadly, I don't have enough free time.

Okay thank you very much for the reply man :)