HTTP GET in VB
HTTP GET in VB
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
leavethatherefornow.hangy Mar23'12at16:07
Whatifthewebpagerequiresausernameandpassword? Matt Jan9'13at18:26
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