HTTP Get Request with Timeout
·
260 words
·
2 minutes read
We’ve already covered how to download a file, but this post covers it a little further by specifying the maximum time attempted on the request. Adding timeouts are often important to stop your program coming to a halt.
To do this we make our own http client, giving it our custom timeout value. We can then use this client to make our requests. If a timeout is reached, the Get()
function will return with an error. Our code is set to a very short timeout of 5 milliseconds to demo the error, but this can be set to any duration you need.
Illustrating the Error
|
|
Successful Example
For the successful example we change the timeout to 10 seconds, which gives it enough time to connect and get a response.
|
|