Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 5.57 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. login with VB.NET and httpwebrequest cookiecontainer
  2. Public Class wiki_submitter
  3. Dim CookieJar As New CookieContainer
  4. Public wikiURL As String
  5.  
  6. Private Function cutStr(ByVal Str As String, ByVal startStr As String, _
  7.                         ByVal finishStr As String, Optional ByVal startPos As Integer = 1) As String
  8.     Dim start As Integer = InStr(startPos, Str, startStr) + Len(startStr)
  9.     Dim finish As Integer = InStr(start + 1, Str, finishStr)
  10.     cutStr = Mid(Str, start, finish - start)
  11. End Function
  12.  
  13. Public Function GetNewForm()
  14.     Try
  15.         Dim req As HttpWebRequest = HttpWebRequest.Create(wikiURL & "index.php?title=Special:UserLogin&returnto=Main_Page")
  16.         Dim res As HttpWebResponse
  17.         req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
  18.         res = req.GetResponse()
  19.         'Setting cookies
  20.         req.CookieContainer = CookieJar
  21.  
  22.         'getting HTML result
  23.         Dim sr As StreamReader = New StreamReader(res.GetResponseStream())
  24.         Dim HTML = sr.ReadToEnd
  25.         sr.Close()
  26.  
  27.  
  28.         Dim wpLoginToken As String = cutStr(HTML, "<input type=""hidden"" name=""wpLoginToken"" value=""", """") ' finding wpLoginToken parameter
  29.         GetNewForm = wpLoginToken
  30.     Catch ex As Exception
  31.         MsgBox(ex.Message)
  32.     End Try
  33. End Function
  34.  
  35. Public Function doLogin(ByVal username As String, ByVal pass As String)
  36.     Dim req As HttpWebRequest = HttpWebRequest.Create(wikiURL & "index.php?title=Special:UserLogin&action=submitlogin&type=login")
  37.     Dim res As HttpWebResponse
  38.     Dim HTML As String
  39.     '-------Setting up headers------------
  40.     req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
  41.     req.Referer = wikiURL & "index.php?title=Special:UserLogin&returnto=Main_Page"
  42.     req.ContentType = "application/x-www-form-urlencoded" 'Form content type
  43.     req.Method = "POST" 'data will be send in POST method
  44.     req.CookieContainer = CookieJar 'Setting cookies
  45.     '-------------------------------------
  46.  
  47.     Dim sw As StreamWriter = New StreamWriter(req.GetRequestStream)
  48.     Dim poststring = "wpLoginToken=" & GetNewForm() & "&wpLoginattempt=Log in&wpName=" & username & "&wpPassword=" & pass
  49.     Try
  50.         sw.Write(poststring)
  51.     Catch ex As Exception
  52.         MsgBox(ex.Message)
  53.     Finally
  54.         sw.Close()
  55.     End Try
  56.  
  57.     res = req.GetResponse()
  58.     Dim sr As StreamReader = New StreamReader(res.GetResponseStream())
  59.     HTML = sr.ReadToEnd 'HTML as result
  60.     sr.Close()
  61.  
  62.     doLogin = HTML 'returns HTML result
  63. End Function
  64. End Class
  65.        
  66. Public Class wiki_submitter
  67.     Dim CookieJar As New CookieContainer
  68.     Public wikiURL As String
  69.  
  70.     Private Function cutStr(ByVal Str As String, ByVal startStr As String, _
  71.                             ByVal finishStr As String, Optional ByVal startPos As Integer = 1) As String
  72.         Dim start As Integer = InStr(startPos, Str, startStr) + Len(startStr)
  73.         Dim finish As Integer = InStr(start + 1, Str, finishStr)
  74.         cutStr = Mid(Str, start, finish - start)
  75.     End Function
  76.  
  77.     Public Function GetNewForm()
  78.         Try
  79.             Dim req As HttpWebRequest = HttpWebRequest.Create(wikiURL & "index.php?title=Special:UserLogin&returnto=Main_Page")
  80.             Dim res As HttpWebResponse
  81.             req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
  82.             res = req.GetResponse()
  83.             'Setting cookies
  84.             'req.CookieContainer = CookieJar
  85.             SaveIncomingCookies(res, wikiURL)
  86.  
  87.             'getting HTML result
  88.             Dim sr As StreamReader = New StreamReader(res.GetResponseStream())
  89.             Dim HTML = sr.ReadToEnd
  90.             sr.Close()
  91.  
  92.  
  93.             Dim wpLoginToken As String = cutStr(HTML, "<input type=""hidden"" name=""wpLoginToken"" value=""", """") ' finding wpLoginToken parameter
  94.             GetNewForm = wpLoginToken
  95.         Catch ex As Exception
  96.             MsgBox(ex.Message)
  97.         End Try
  98.     End Function
  99.  
  100.     Public Function doLogin(ByVal username As String, ByVal pass As String)
  101.         Dim req As HttpWebRequest = HttpWebRequest.Create(wikiURL & "index.php?title=Special:UserLogin&action=submitlogin&type=login")
  102.         Dim res As HttpWebResponse
  103.         Dim HTML As String
  104.         '-------Setting up headers------------
  105.         req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
  106.         req.Referer = wikiURL & "index.php?title=Special:UserLogin&returnto=Main_Page"
  107.         req.ContentType = "application/x-www-form-urlencoded" 'Form content type
  108.         req.Method = "POST" 'data will be send in POST method
  109.         'req.CookieContainer = CookieJar 'Setting cookies
  110.         '-------------------------------------
  111.  
  112.         Dim sw As StreamWriter = New StreamWriter(req.GetRequestStream)
  113.         Dim poststring = "wpLoginToken=" & GetNewForm() & "&wpLoginattempt=Log in&wpName=" & username & "&wpPassword=" & pass
  114.         Try
  115.             req.CookieContainer = CookieJar
  116.             sw.Write(poststring)
  117.         Catch ex As Exception
  118.             MsgBox(ex.Message)
  119.         Finally
  120.             sw.Close()
  121.         End Try
  122.  
  123.         res = req.GetResponse()
  124.         SaveIncomingCookies(res, wikiURL)
  125.         Dim sr As StreamReader = New StreamReader(res.GetResponseStream())
  126.         HTML = sr.ReadToEnd 'HTML as result
  127.         sr.Close()
  128.  
  129.         doLogin = HTML 'returns HTML result
  130.     End Function
  131.  
  132.     Private Function SaveIncomingCookies(ByRef response As HttpWebResponse, ByRef Uri As String)
  133.  
  134.  
  135.         If response.Headers("Set-Cookie") <> Nothing Then
  136.             CookieJar.SetCookies(New Uri("http://wetcatdesign.com"), response.Headers("Set-Cookie"))
  137.         End If
  138.  
  139.     End Function
  140. End Class