httpwebrequest 3 requests timeout problem in vb.net

I recently wrote a tool to auto-post information to Twitter every 3 minutes. However I hit a problem that took me a week to figure out.

Each time the program would work fine for the first two or so posts, then shortly after it would start showing a Timeout instead of posting. Wireshark would show no attempt to contact Twitter!

After lots of searching on google and trying to find advice. Lots of which were set the timeout, set the servicepointmanager timeout, ServicePointManager.DefaultConnectionLimit etc..

A lot of places suggested using GC.Collect (Garbage Collection) after each post. This also didn’t help.

What finally did solve the problem was simple.. Changing my code from this:

dim response As WebResponse = request.GetResponse()
Return response.ToString
response.Close()
response = nothing
GC.Collect()

To:

Using response As WebResponse = request.GetResponse()
    Return response.ToString
    response.Close()
End Using
GC.Collect()

Even though I was closing response and setting it to nothing somehow something about the “Using” clause does better connection tidying or thread control.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Comment on this topic

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s