Answered Web Access in code

AccessShell

Member
Joined
Jun 14, 2016
Messages
21
Programming Experience
10+
In VB6. Notice the last statement

Set HttpRequest = New WinHttp.WinHttpRequest
With HttpRequest
.Open "GET", "http://....................... ", True
.Send
If .WaitForResponse(3) Then
Do Something . . . . . . . .
holdResponse = .ResponseText
End If
End With
Set HttpRequest = Nothing

But in VB.NET

sURL = “http://........................ “
dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)

Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()

Dim objReader As New StreamReader(objStream)

holdResponse = objReader.ReadToEnd

Do I have close the request? I haven’t found anything on the web.

Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,283
Location
Norway
Programming Experience
10+
Close the StreamReader (or put it in Using block), that will also close the response stream it is reading from, so that is the only call that is needed.

What to look out for in .Net is disposable objects, they implement IDisposable interface with a Dispose method. Some of these also have Close method that can be used interchangeably. If the object is created by your code and can be disposed then you must do so before releasing the reference to it.

Going through your code:
WebRequest.Create with a http url creates a HttpWebRequest, it has no Dispose method.
GetResponse then returns a HttpWebResponse object that is disposable, GetResponseStream returns a Stream that is disposable. Documentation explains either one of these must be disposed or closed.
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,283
Location
Norway
Programming Experience
10+
We leave threads open here, but thanks for letting us know you found the answer useful.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,100
Location
Sydney, Australia
Programming Experience
10+
Thank you

How do I close the thread as answered?
When you start a thread, you should specify the Question prefix if you're asking a question. If you resolve your own issue or receive an appropriate answer then you can edit and change the prefix to Resolved or Answered. As suggested, the thread still remains open, so you or someone else can add something further if appropriate, but it is obvious to all that your question isn't still pending without opening the thread and reading the whole thing.
 
Top Bottom