Results 1 to 3 of 3

Thread: How to get Wan?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    How to get Wan?

    Hello again,

    I'm attempting to get my computers public IP (WAN) and I've used numerous codes suggested from the net and none of them seem to work!

    Example #1:
    Code:
      Function GetIpAddress() As String
            Dim ip As New WebClient
            Return ip.DownloadString("http://automation.whatismyip.com/n09230945.asp")
        End Function
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  Me.GetIpAddress().ToString()
            MessageBox.Show(GetIpAddress())
        End Sub
    Example #2:
    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim WC As New System.Net.WebClient
            Label2.Text =
            System.Text.Encoding.ASCII.GetString((WC.DownloadData("http://whatismyip.com/automation/n09230945.asp")))
            WC.Dispose()
            End Sub
    Example #3:
    Code:
     'Dim ip() As Net.IPAddress = System.Net.Dns.GetHostAddresses("http://***ismyip.com/text")
        '
        'If ip.Count > 0 Then
        'For Each ipadd As Net.IPAddress In ip
        'Console.WriteLine(ipadd.ToString)
        'Next
        'End If
    Example #4
    Code:
     'Dim req As HttpWebRequest = WebRequest.Create("http://***ismyip.com/text")
        'Dim res As HttpWebResponse = req.GetResponse()
        'Dim Stream As Stream = res.GetResponseStream()
        'Dim sr As StreamReader = New StreamReader(Stream)
        '   MessageBox.Show(sr.ReadToEnd())
    When I press Button #3, nothing happens. Nothing. And I tested button 3 with other code to make sure that wasn't the issue and it isn't. If the code(s) should be working, then perhaps it's a router/firewall issue? Any help very much appreciated.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: How to get Wan?

    Gotta give credit where credit is due; this code seems to work and the code is from:

    Router IP ermitteln. Ellen Ramcke 2011
    Code:
    Imports System.Net
    Imports System.IO
    
    Public Class Form1
        Dim PublicIP As String
    
        Private Sub DOIT()
            Dim client As New WebClient
            '// Add a user agent header in case the requested URI contains a query.
            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)")
            Dim baseurl As String = "http://checkip.dyndns.org/"
            ' with proxy server only:
            Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
            proxy.Credentials = CredentialCache.DefaultNetworkCredentials
            client.Proxy = proxy
            Dim data As Stream
            Try
                data = client.OpenRead(baseurl)
            Catch ex As Exception
                MsgBox("open url " & ex.Message)
                Exit Sub
            End Try
            Dim reader As StreamReader = New StreamReader(data)
            Dim s As String = reader.ReadToEnd()
            data.Close()
            reader.Close()
            s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString()
            MessageBox.Show(s)
            PublicIP = s
            'Me.Text = s
        End Sub
    
        Private Sub btnGetIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetIP.Click
            Call DOIT()
            'TAKES PUBLIC VARIABLE PUBLICIP FROM FUNCTION AND GIVE IT TO THIS SUB THEN PUT TP IN WINDOW BAR
            Me.Text = PublicIP
        End Sub
    
    End Class
    I modified the original code a bit to see if I could call it as a sub and I could, then see if I could declare a public variable and use it in the function then use it with button_click. All seems to work well. Before I resolve this, I'd still like to know why the other codes won't work when everything I've read says they should .

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: How to get Wan?

    As you have your button3 code handling Button1.Click, I'm not exactly surprised that Button 3 gives nothing when pressed!!!

    #1 & #2 merely return the script or source HTML as text so I've no idea how anybody expected that to work. I imagine that whatismyip does have some kind of API but this certainly isn't how to use it! #3 & #4 don't even have the correct web address but suffer from the same delusion that you can simply knock on the site's 'door' and it will give you exactly what you want.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Posting Permissions

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



Click Here to Expand Forum to Full Width