title: How to log into router with HttpWebRequest?
body: <p>I'm trying to figure out how to successfully log into a DSL-Router (Model: Speedport w504v Type A).
  I wrote a function usinig HttpWebRequest and HttpWebResponse. So far this function is not finished and is only for finding out the right login process.</p>
  
  <pre><code>Public Function DoRequest(ByVal url As String, ByVal password As String, ByVal container As CookieContainer) As String
  
      'Login Request
      Dim reqLogin As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://speedport.ip/cgi-bin/login.cgi"), HttpWebRequest)
      reqLogin.CookieContainer = container
      reqLogin.Method = "POST"
      reqLogin.Referer = "https://speedport.ip/hcti_start_passwort.stm"
      reqLogin.KeepAlive = True
      reqLogin.Host = "speedport.ip"
      reqLogin.ContentType = "application/x-www-form-urlencoded"
      reqLogin.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      reqLogin.Headers.Add("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3")
      reqLogin.Headers.Add("Accept-Encoding", "gzip, deflate")
  
      'Login Data
      Dim encoding As New ASCIIEncoding()
      Dim data As String = Uri.EscapeDataString("pws") &amp; "=" &amp; Uri.EscapeDataString(password)
      Dim bytes As Byte() = encoding.GetBytes(data)
      reqLogin.ContentLength = bytes.Length
      Dim stream As Stream = reqLogin.GetRequestStream
      stream.Write(bytes, 0, bytes.Length)
      stream.Close()
  
      'Login Response
      Dim resLogin As HttpWebResponse = DirectCast(reqLogin.GetResponse(), HttpWebResponse)
  
      'Receive Cookie
      Dim CookieHeaderValue As String = reqLogin.Headers.Get("Cookie")
      If CookieHeaderValue &lt;&gt; Nothing Then
          Dim aCookie As String() = CookieHeaderValue.Split("=")
          Dim Cookie As New Cookie
          Cookie.Domain = "speedport.ip"
          Cookie.Path = "/"
          Cookie.Secure = True
          Cookie.Name = aCookie(0)
          Cookie.Value = aCookie(1)
          container.Add(Cookie)
      End If
  
      'Url Request
      Dim reqIndex As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
      reqIndex.CookieContainer = container
      reqIndex.Method = "GET"
      reqIndex.Referer = "https://speedport.ip/wait_login.stm"
      reqIndex.KeepAlive = True
      reqIndex.Host = "speedport.ip"
      reqIndex.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  
      'Url Content
      Dim resIndex As HttpWebResponse = DirectCast(reqIndex.GetResponse(), HttpWebResponse)
      Dim sr As StreamReader = New StreamReader(resIndex.GetResponseStream())
      Dim output As String = sr.ReadToEnd
      resIndex.Close()
  
      Return output
  End Function
  </code></pre>
  
  <p>Unfortunately I don't get the right content. Instead I'm getting the sitecontent from an Error-Page saying:</p>
  
  <blockquote>
    <p>double administration access!</p>
  </blockquote>
  
  <p>This site is returned when you try to login but there is another session already running.
  So maybe I'm already successfully logged in but don't get the site content.</p>
  
  <p>I get the header information from an Firefox AddOn called HTTP Live Header.
  I also tried to run curl but also did not work:</p>
  
  <pre><code>curl -d "pws=PASSWORD" -c cookies.txt -e https://speedport.ip/hcti_start_passwort.stm -k https://speedport.ip/cgi-bin/login.cgi
  curl -c cookies.txt -e https://speedport.ip/wait_login.stm -k https://speedport.ip/index.stm
  </code></pre>
  
  <p>Maybe someone has an idea what's going wrong.</p>