Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Parsing text from a webpage
Message
De
07/05/2007 10:33:47
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
 
 
À
07/05/2007 09:58:27
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
ASP.NET
Divers
Thread ID:
01223022
Message ID:
01223031
Vues:
13
>I think I am trying too hard, but I just can't figure this out and am hoping somebody can point me in the right direction.
>
>I am trying to obtain the ip address displayed on http://whatismyip.org/ into my application. I found this snippet on the internet:
>
>
>Dim value As String
>Dim webClient As New System.Net.WebClient
>
>value = New System.Text.UTF8Encoding().GetString(webClient.DownloadData("http://whatismyip.org/"))
>
>
>but I am getting an error. Is there an easier way to do this? There is no HTML - the source is just the ip address.

I found my solution. I had to add the user agent header:
    Public Shared Function GetExternalIP() As String

        Dim value As String
        Dim webClient As System.Net.WebClient

        Try
            'Instantiate objects
            webClient = New System.Net.WebClient

            'Add header to request
            webClient.Headers.Add(Net.HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")

            'Retrieves the value from whatismyip.org
            value = New System.Text.UTF8Encoding().GetString(webClient.DownloadData("http://whatismyip.org/"))
        Catch ex As Exception
            Throw New ApplicationException("Unable to load external IP address.")
        End Try

        Return value

    End Function
Very fitting: http://xkcd.com/386/
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform