0% found this document useful (0 votes)
178 views

HTTP GET in VB

To issue an HTTP GET request in VB.NET, use the WebClient or HttpWebRequest classes. WebClient provides an easier interface by allowing you to directly download a string from a URL using DownloadString. HttpWebRequest provides more flexibility to customize requests but requires more code. Both classes can be used to make GET requests and retrieve response contents in VB.NET.

Uploaded by

subhramaiti0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views

HTTP GET in VB

To issue an HTTP GET request in VB.NET, use the WebClient or HttpWebRequest classes. WebClient provides an easier interface by allowing you to directly download a string from a URL using DownloadString. HttpWebRequest provides more flexibility to customize requests but requires more code. Both classes can be used to make GET requests and retrieve response contents in VB.NET.

Uploaded by

subhramaiti0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

signup

login

tour

help
Dismiss

AnnouncingStackOverflowDocumentation
WestartedwithQ&A.Technicaldocumentationisnext,andweneedyourhelp.
Whetheryou'reabeginneroranexperienceddeveloper,youcancontribute.

Signupandstarthelping

LearnmoreaboutDocumentation

HTTPGETinVB.NET
WhatisthebestwaytoissueahttpgetinVB.net?Iwanttogettheresultofarequestlikehttp://api.hostip.info/?
ip=68.180.206.184
vb.net httpget
askedSep18'08at13:28

notandy
2,542

6Answers

InVB.NET:
DimwebClientAsNewSystem.Net.WebClient
DimresultAsString=webClient.DownloadString("http://api.hostip.info/?
ip=68.180.206.184")

InC#:
System.Net.WebClientwebClient=newSystem.Net.WebClient();
stringresult=webClient.DownloadString("http://api.hostip.info/?ip=68.180.206.184");
editedMar23'12at16:05

answeredSep18'08at13:31

hangy
8,502

31

53

DimwebClientAsSystem.Net.WebClient=NewSystem.Net.WebClient() canbeabbreviatedto Dim


webClientAsNewSystem.Net.WebClient can'tit? MattLyons Nov29'11at22:36

@MattLyonsYes,itcan.Onecouldalsoomit AsString from DimresultAsString=... ,butI'lljust

leavethatherefornow.hangy Mar23'12at16:07
Whatifthewebpagerequiresausernameandpassword? Matt Jan9'13at18:26

1 @MattUse HttpWebRequest andsetthe Credentials propertytoanewinstanceof


NetworkCredential . hangy Jan9'13at19:00

YoucanusetheHttpWebRequestclasstoperformarequestandretrievearesponsefroma
givenURL.You'lluseitlike:
Try
DimfrAsSystem.Net.HttpWebRequest
DimtargetURIAsNewUri("http://whatever.you.want.to.get/file.html")
fr=DirectCast(HttpWebRequest.Create(targetURI),System.Net.HttpWebRequest)
If(fr.GetResponse().ContentLength>0)Then
DimstrAsNewSystem.IO.StreamReader(fr.GetResponse().GetResponseStream())
Response.Write(str.ReadToEnd())
str.Close();
EndIf
CatchexAsSystem.Net.WebException
'Errorinaccessingtheresource,handleit
EndTry

HttpWebRequestisdetailedat:http://msdn.microsoft.com/en
us/library/system.net.httpwebrequest.aspx
AsecondoptionistousetheWebClientclass,thisprovidesaneasiertouseinterfacefor
downloadingwebresourcesbutisnotasflexibleasHttpWebRequest:
SubMain()
'AddressofURL
DimURLAsString=http://whatever.com
'GetHTMLdata
DimclientAsWebClient=NewWebClient()

17

27

DimdataAsStream=client.OpenRead(URL)
DimreaderAsStreamReader=NewStreamReader(data)
DimstrAsString=""
str=reader.ReadLine()
DoWhilestr.Length>0
Console.WriteLine(str)
str=reader.ReadLine()
Loop
EndSub

Moreinfoonthewebclientcanbefoundat:http://msdn.microsoft.com/en
us/library/system.net.webclient.aspx
editedSep18'08at13:48

answeredSep18'08at13:37

Wolfwyrd
10k

26

1 Dropthesemicolonfromstr.Close()infirstexamplethenallgood.CorgaloreOct31'11at19:42
WebClientisaquicksolution,buttheHttpWebRequestismorepowerful.InaprojectIneededtoget

imagesmetadatafromremoteresources:Iavoidedtodownloadtheimagesintothefs,andIusedthe
ResponseStreaminsted.AlbertoDeCaroJun6'12at13:51
1 Response.Write(str.ReadToEnd()) assumessheisusingasp.net.MaxHodges Jan27'14at13:10

usethewebrequestclass
thisistogetanimage
Try
Dim_WebRequestAsSystem.Net.WebRequest=Nothing
_WebRequest=System.Net.WebRequest.Create(http://api.hostip.info/?ip=68.180.206.184)
CatchexAsException
Windows.Forms.MessageBox.Show(ex.Message)
ExitSub
EndTry
Try
_NormalImage=
Image.FromStream(_WebRequest.GetResponse().GetResponseStream())
CatchexAsException
Windows.Forms.MessageBox.Show(ex.Message)
ExitSub
EndTry
answeredSep18'08at13:32

chrissie1
3,247

Theeasiestwayis
DownloadString .

System.Net.WebClient.DownloadFile

editedJun9'12at22:31
11

17

22

or

answeredSep18'08at13:32

SiddharthRout
89.8k

OliverMellet

102

145

1,506

13

YoushouldtrytheHttpWebRequest
class.
answeredSep18'08at13:29

DarioSolera
3,462

19

32

Trythis:
WebRequestrequest=WebRequest.CreateDefault(RequestUrl);
request.Method="GET";
WebResponseresponse;
try{response=request.GetResponse();}
catch(WebExceptionexc){response=exc.Response;}
if(response==null)
thrownewHttpException((int)HttpStatusCode.NotFound,"Therequestedurlcouldnotbe
found.");
using(StreamReaderreader=newStreamReader(response.GetResponseStream())){
stringrequestedText=reader.ReadToEnd();
//dowhatyouwantwithrequestedText
}

SorryabouttheC#,IknowyouaskedforVB,butIdidn'thavetimetoconvert.

61

answeredSep18'08at13:36

NickBerardi
39.1k

12

93

122

You might also like