Sixem

.NET Tumblr Login HTTPWebRequests

Aug 10th, 2013
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.24 KB | None | 0 0
  1. 'Written By Sixem
  2. Imports System.Net
  3. Imports System.Text
  4. Imports System.IO
  5. Public Class TumblrAPI
  6.     Public Shared User_Agent As String = CStr("Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)")
  7.     Public Shared TumblrCookies As New CookieContainer
  8.     Public Shared Function Login(ByVal Email_ As String, ByVal Pass_ As String)
  9.         Dim Get_Key As String = Get_Data("https://www.tumblr.com/login")
  10.         Dim Extracted_Key As String = CStr(GetBetween(Get_Key, "data-form-key=", ">")).Replace("""", Nothing)
  11.         Dim PostData As String = String.Format("user%5Bemail%5D={0}&user%5Bpassword%5D={1}&tumblelog%5Bname%5D=&user%5Bage%5D=&recaptcha_public_key=&recaptcha_response_field=&context=home_signup&follow=&http_referer=http://www.tumblr.com&form_key={2}&seen_suggestion=0&used_suggestion=0", Email_, Pass_, Extracted_Key)
  12.         Post("https://www.tumblr.com/login", PostData)
  13.         Return Get_Data("https://www.tumblr.com/dashboard")
  14.     End Function
  15.     Public Shared Function Get_Data(ByVal Site_ As String)
  16.         Dim request As HttpWebRequest = DirectCast(WebRequest.Create(Site_), HttpWebRequest)
  17.         request.CookieContainer = TumblrCookies
  18.         Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
  19.         Dim reader As New StreamReader(response.GetResponseStream())
  20.         Return CStr(reader.ReadToEnd)
  21.     End Function
  22.     Public Shared Function Post(ByVal Site_ As String, ByVal Post_ As String)
  23.         Dim postData As String = Post_
  24.         Dim tempCookies As New CookieContainer
  25.         Dim _Encryption As New UTF8Encoding
  26.         Dim byteData As Byte() = _Encryption.GetBytes(postData)
  27.         Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(Site_), HttpWebRequest)
  28.         postReq.Method = "POST"
  29.         postReq.KeepAlive = True
  30.         postReq.CookieContainer = tempCookies
  31.         postReq.ContentType = "application/x-www-form-urlencoded"
  32.         postReq.Referer = Site_
  33.         postReq.UserAgent = User_Agent
  34.         postReq.ContentLength = byteData.Length
  35.         postReq.AllowAutoRedirect = True
  36.         Dim postreqstream As Stream = postReq.GetRequestStream()
  37.         postreqstream.Write(byteData, 0, byteData.Length)
  38.         postreqstream.Close()
  39.         Dim postresponse As HttpWebResponse
  40.         postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
  41.         tempCookies.Add(postresponse.Cookies)
  42.         TumblrCookies = tempCookies
  43.         Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
  44.         Return CStr(postreqreader.ReadToEnd)
  45.     End Function
  46.     Public Shared Function GetBetween(ByRef strSource As String, ByRef strStart As String, ByRef strEnd As String, _
  47.     Optional ByRef startPos As Integer = 0) As String
  48.         Dim iPos As Integer, iEnd As Integer, lenStart As Integer = strStart.Length
  49.         Dim strResult As String
  50.         strResult = String.Empty
  51.         iPos = strSource.IndexOf(strStart, startPos)
  52.         iEnd = strSource.IndexOf(strEnd, iPos + lenStart)
  53.         If iPos <> -1 AndAlso iEnd <> -1 Then
  54.             strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart))
  55.         End If
  56.         Return strResult
  57.     End Function
  58. End Class
Advertisement
Add Comment
Please, Sign In to add comment