2012-06-27 82 views
2

我想弄清楚如何成功登錄到DSL路由器(型號:Speedport w504v類型A)。 我寫了一個函數usinig HttpWebRequest和HttpWebResponse。到目前爲止,這個功能還沒有完成,只是爲了找到正確的登錄過程。如何使用HttpWebRequest登錄路由器?

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 -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 

也許有人有一個想法是什麼地方出了錯。

回答

0

使用Fiddler我想通了。我還需要爲第一個請求設置User Agent Header。

reqLogin.UserAgent = "YOUR USERAGNET STRING" 

此外,我沒有得到reqLogin.Headers.Get("Cookie")的值。 它已經設置爲CookieContainer,因此取消「接收Cookie」部分。