Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184")

In C#:

System.Net.WebClient webClient = new System.Net.WebClient();
string result = webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184");
                Dim webClient As System.Net.WebClient = New System.Net.WebClient() can be abbreviated to Dim webClient As New System.Net.WebClient can't it?
– Matt Lyons-Wood
                Nov 29, 2011 at 22:36
                @MattLyons Yes, it can. One could also omit As String from Dim result As String = ..., but I'll just leave that here for now.
– hangy
                Mar 23, 2012 at 16:07
                @Matt Use HttpWebRequest and set the Credentials property to a new instance of NetworkCredential.
– hangy
                Jan 9, 2013 at 19:00

You can use the HttpWebRequest class to perform a request and retrieve a response from a given URL. You'll use it like:

Dim fr As System.Net.HttpWebRequest Dim targetURI As New Uri("http://whatever.you.want.to.get/file.html") fr = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest) If (fr.GetResponse().ContentLength > 0) Then Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream()) Response.Write(str.ReadToEnd()) str.Close(); End If Catch ex As System.Net.WebException 'Error in accessing the resource, handle it End Try

HttpWebRequest is detailed at: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx

A second option is to use the WebClient class, this provides an easier to use interface for downloading web resources but is not as flexible as HttpWebRequest:

Sub Main()
    'Address of URL
    Dim URL As String = http://whatever.com
    ' Get HTML data
    Dim client As WebClient = New WebClient()
    Dim data As Stream = client.OpenRead(URL)
    Dim reader As StreamReader = New StreamReader(data)
    Dim str As String = ""
    str = reader.ReadLine()
    Do While str.Length > 0
        Console.WriteLine(str)
        str = reader.ReadLine()
End Sub

More info on the webclient can be found at: http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

WebClient is a quick solution, but the HttpWebRequest is more powerful. In a project I needed to get images metadata from remote resources: I avoided to download the images into the fs, and I used the ResponseStream insted. – Alberto De Caro Jun 6, 2012 at 13:51 @marshalcraft - should work fine for https see stackoverflow.com/questions/560804/… for pointers if it's not working for you – Wolfwyrd Jan 31, 2017 at 15:10 Dim _WebRequest As System.Net.WebRequest = Nothing _WebRequest = System.Net.WebRequest.Create(http://api.hostip.info/?ip=68.180.206.184) Catch ex As Exception Windows.Forms.MessageBox.Show(ex.Message) Exit Sub End Try _NormalImage = Image.FromStream(_WebRequest.GetResponse().GetResponseStream()) Catch ex As Exception Windows.Forms.MessageBox.Show(ex.Message) Exit Sub End Try WebResponse response; try { response = request.GetResponse(); } catch (WebException exc) { response = exc.Response; } if (response == null) throw new HttpException((int)HttpStatusCode.NotFound, "The requested url could not be found."); using(StreamReader reader = new StreamReader(response.GetResponseStream())) { string requestedText = reader.ReadToEnd(); // do what you want with requestedText

Sorry about the C#, I know you asked for VB, but I didn't have time to convert.

Public Function getLoginresponce(ByVal email As String, ByVal password As String) As String
    Dim requestUrl As String = "your api"
    Dim request As HttpWebRequest = TryCast(WebRequest.Create(requestUrl), HttpWebRequest)
    Dim response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
    Dim dataStream As Stream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Dim result = responseFromServer
    reader.Close()
    response.Close()
    Return result
End Function
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.