2

我试图弄清楚如何成功登录 DSL 路由器(型号:Speedport w504v A 型)。我使用 HttpWebRequest 和 HttpWebResponse 编写了一个函数。到目前为止,这个功能还没有完成,只是为了找出正确的登录过程。

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") & "=" & 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 <> 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

不幸的是,我没有得到正确的内容。相反,我从错误页面获取站点内容:

双重管理权限!

当您尝试登录时返回此站点,但已经有另一个会话在运行。所以也许我已经成功登录但没有获得网站内容。

我从一个名为 HTTP Live Header 的 Firefox AddOn 获取标题信息。我也尝试运行 curl 但也没有工作:

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

也许有人知道出了什么问题。

4

1 回答 1

0

使用 Fiddler 我想通了。我还必须为第一个请求设置用户代理标头。

reqLogin.UserAgent = "YOUR USERAGNET STRING"

此外,我不必获得reqLogin.Headers.Get("Cookie"). 它已经设置为 CookieContainer,因此取消“接收 Cookie”部分。

于 2012-06-28T10:40:56.867 回答