49

What is the best way to issue a http get in VB.net? I want to get the result of a request like http://api.hostip.info/?ip=68.180.206.184

notandy
  • 3,330
  • 1
  • 27
  • 35

7 Answers7

78

In VB.NET:

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");
hangy
  • 10,765
  • 6
  • 43
  • 63
  • 2
    `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 '11 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 '12 at 16:07
  • 1
    What if the webpage requires a username and password? – Matt Jan 09 '13 at 18:26
  • 2
    @Matt Use [`HttpWebRequest`](http://stackoverflow.com/a/92588/11963) and set the [`Credentials`](http://msdn.microsoft.com/library/system.net.httpwebrequest.credentials.aspx) property to a new instance of [`NetworkCredential`](http://msdn.microsoft.com/library/system.net.networkcredential.aspx). – hangy Jan 09 '13 at 19:00
27

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

Try
    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()
    Loop
End Sub

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

Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
  • 1
    Drop the semi-colon from str.Close(); in first example then all good. – Corgalore Oct 31 '11 at 19:42
  • 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 06 '12 at 13:51
  • 1
    `Response.Write(str.ReadToEnd())` assumes she is using asp.net. – ChatGPT Jan 27 '14 at 13:10
  • @Wolfwyrd will this work with `https` or does one have to use additional api. – marshal craft Jan 28 '17 at 15:14
  • @marshalcraft - should work fine for https see http://stackoverflow.com/questions/560804/how-do-i-use-webrequest-to-access-an-ssl-encrypted-site-using-https for pointers if it's not working for you – Wolfwyrd Jan 31 '17 at 15:10
5

Use the WebRequest class

This is to get an image:

Try
    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

Try
    _NormalImage = Image.FromStream(_WebRequest.GetResponse().GetResponseStream())
Catch ex As Exception
    Windows.Forms.MessageBox.Show(ex.Message)
    Exit Sub
End Try
Jimi
  • 29,621
  • 8
  • 43
  • 61
chrissie1
  • 5,014
  • 3
  • 26
  • 26
3

The easiest way is System.Net.WebClient.DownloadFile or DownloadString.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Oliver Mellet
  • 2,389
  • 12
  • 10
1

You should try the HttpWebRequest class.

Dario Solera
  • 5,694
  • 3
  • 29
  • 34
1

Try this:

WebRequest request = WebRequest.CreateDefault(RequestUrl);
request.Method = "GET";

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.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
0
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
sanket parikh
  • 281
  • 2
  • 8