dcsimg
Results 1 to 7 of 7

Thread: Communication with the Huawei router

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    9

    Communication with the Huawei router

    Hi All,

    I am trying to extract data from the router using the API (you can find same information here)
    If I send 192.168.8.1/api/dhcp/settings in the browser I get the answer as below.


    <?xml version="1.0" encoding="UTF-8"?>
    <response>
    <DhcpIPAddress>192.168.8.1</DhcpIPAddress>
    <DhcpLanNetmask>255.255.255.0</DhcpLanNetmask>
    <DhcpStatus>1</DhcpStatus>
    <DhcpStartIPAddress>192.168.8.100</DhcpStartIPAddress>
    <DhcpEndIPAddress>192.168.8.200</DhcpEndIPAddress>
    <DhcpLeaseTime>86400</DhcpLeaseTime>
    <DnsStatus>1</DnsStatus>
    <PrimaryDns>192.168.8.1</PrimaryDns>
    <SecondaryDns>192.168.8.1</SecondaryDns>
    <ShowDnsSetting>0</ShowDnsSetting>
    </response>
    When I try to do the same with VB I get an error. I use the code as below.
    Can anyone see what I'm doing wrong?

    Code:
    Shared Function HttpGet(URL As String) As String
            Dim request As WebRequest = WebRequest.Create(URL)
            request.Credentials = CredentialCache.DefaultCredentials
            Dim response As WebResponse = request.GetResponse()
            If CType(response, HttpWebResponse).StatusCode = HttpStatusCode.OK Then
                Dim dataStream As Stream = response.GetResponseStream()
                Dim reader As New StreamReader(dataStream)
                Dim responseFromServer As String = reader.ReadToEnd()
                reader.Close()
                response.Close()
                Return responseFromServer
            Else
                Return ""
            End If
        End Function
    
        Private Sub bt_test_Click(sender As Object, e As EventArgs) Handles bt_test.Click
            MsgBox(HttpGet("http://192.168.8.1/api/dhcp/settings"))
        End Sub
    The router's response below.

    <?xml version="1.0" encoding="UTF-8"?>
    <error>
    <code>100003</code>
    <message></message>
    </error>

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,063

    Re: Communication with the Huawei router

    Try this
    Code:
            Dim xe As XElement = XElement.Load("192.168.8.1/api/dhcp/settings")
    
            'for testing
            'xe = <response>
            '         <DhcpIPAddress>192.168.8.1</DhcpIPAddress>
            '         <DhcpLanNetmask>255.255.255.0</DhcpLanNetmask>
            '         <DhcpStatus>1</DhcpStatus>
            '         <DhcpStartIPAddress>192.168.8.100</DhcpStartIPAddress>
            '         <DhcpEndIPAddress>192.168.8.200</DhcpEndIPAddress>
            '         <DhcpLeaseTime>86400</DhcpLeaseTime>
            '         <DnsStatus>1</DnsStatus>
            '         <PrimaryDns>192.168.8.1</PrimaryDns>
            '         <SecondaryDns>192.168.8.1</SecondaryDns>
            '         <ShowDnsSetting>0</ShowDnsSetting>
            '     </response>
    
            Debug.WriteLine(xe.<DhcpStartIPAddress>.Value)
    edit: After the fact I clicked on the link in your OP that purported to show the response you showed. I did not find that.
    Last edited by dbasnett; Dec 14th, 2017 at 09:52 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,944

    Re: Communication with the Huawei router

    Quote Originally Posted by dbasnett View Post
    edit: After the fact I clicked on the link in your OP that purported to show the response you showed. I did not find that.
    It's a 192.168 address... which is typically an internal LAN address, so I wouldn't expect anyone else to also get it, unless they were also on an internal network with the same router with the same gateway setup with the same IP address... which isn't likely.

    Either way, it should work... I'd be interested to see if there's a difference in whether .Load version works or not. I suspect it won't.

    @Artek - do you first have to login to the router before getting that information?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,063

    Re: Communication with the Huawei router

    Quote Originally Posted by techgnome View Post
    It's a 192.168 address... which is typically an internal LAN address, so I wouldn't expect anyone else to also get it, unless they were also on an internal network with the same router with the same gateway setup with the same IP address... which isn't likely.

    Either way, it should work... I'd be interested to see if there's a difference in whether .Load version works or not. I suspect it won't.

    @Artek - do you first have to login to the router before getting that information?

    -tg
    My edit was about the link the OP posted. In it I did not find an example of the XML that the OP posted.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,944

    Re: Communication with the Huawei router

    @dbassnet = my bad, I misunderstood your statement and almost missed his link...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Communication with the Huawei router

    The documentation you linked to says every request requires a valid session cookie to be set, but your code isn't setting any cookies.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    9

    Re: Communication with the Huawei router

    Quote Originally Posted by Sitten Spynne View Post
    The documentation you linked to says every request requires a valid session cookie to be set, but your code isn't setting any cookies.
    Most likely this is the cause of my problem.The browser showed something different because I was logged into the router.
    I noticed that sending to the router 192.168.8.1/api/webserver/SesTokInfo/ get the answer from the session ID and token. It works in both the browser and my code.

    <?xml version="1.0" encoding="UTF-8"?>
    <response>
    <SesInfo>SessionID=odarWqk6PAT8SQUMaANmD60DHeAtQs/y8fRspHokPnIfcaGfvpgWc9eDzovlLxUrT2RAbvkbSFsyeILlzBFiDYPUny9EI1BfyDk95RiGIp1G/tzXiWtWZkDRnrNx5vrx</SesInfo>
    <TokInfo>vmfbINhiOnuZDY6QZbM5OUlzE5xRoDUf</TokInfo>
    </response>
    In summary It looks like I need to log in first and also use authorization (cookie and token?) in communication with router.
    Any ideas how to implement this?


    Update 21.12.2017
    Maybe I will ask in a different way. When I use CURL as below the server responds correctly.

    curl 'http://192.168.8.1/api/user/login' \
    -H 'Cookie: SessionID=CFb04V65q40d59bT4iIWvedI1IGnO98YIPNzkaQ5kvM6BM75H+ujI1IKTf0od9auwFO96/oUjWXL+ZB+dKFyRO8djfYHSWy39UCjuaDXCP8M5t1zKv/C+oaHbMpeht58' \
    -H '__RequestVerificationToken: ELHGAIDDKwtV59FVtj6UsWTpFJVUOiNU' \
    --data '<?xml version="1.0" encoding="UTF-8"?><<request><Username>admin</Username><Password>YWRtaW4=</Password><password_type>4</password_type></request>'
    Trying to do the same in VB is still getting an error (I have a valid session ID and token).

    <?xml version="1.0" encoding="UTF-8"?>
    <error>
    <code>125002</code>
    <message></message>
    </error>
    Code:
    Dim webRequest As HttpWebRequest = HttpWebRequest.Create("http://192.168.8.1/api/user/login/")
    
            webRequest.Method = "POST"
            webRequest.Headers.Add("Cookie: " & session)
            webRequest.Headers.Add("__RequestVerificationToken: " & token)
    
            Dim postString As String = "<?xml version=""1.0"" encoding=""UTF-8""?><<request><Username>admin</Username><Password>YWRtaW4=</Password><password_type>4</password_type></request>"
            webRequest.ContentLength = postString.Length
            webRequest.ContentType = "application/x-www-form-urlencoded"
            Dim requestWriter As StreamWriter = New StreamWriter(webRequest.GetRequestStream())
            requestWriter.Write(postString)
            requestWriter.Close()
    
            Dim responseReader As StreamReader = New StreamReader(webRequest.GetResponse().GetResponseStream())
            Dim responseData As String = responseReader.ReadToEnd()
    
            MsgBox(responseData)
            responseReader.Close()
            webRequest.GetResponse().Close()
    Is the above code correctly written? I'm stuck on it and I do not know what's wrong here.
    Last edited by Artek; Dec 21st, 2017 at 07:23 AM. Reason: Update

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width