Closing requests

Asked

Viewed 85 times

3

I currently make several HTTP/HTTPS requests to different websites using the classes HttpWebRequest and HttpWebResponse using framework .NET 4.0*. We are facing a problem when there comes a time we cannot acquire SSL/TLS connection , as if we were coming to a bottleneck. I would like to know from you if when a request , made as below, entails error (entering a catch) and proceeding to another so successively, whether such a request is closed or not.

  PostData &= "&" & System.Web.HttpUtility.UrlEncode("btnOK", System.Text.Encoding.UTF8) & "=" & System.Web.HttpUtility.UrlEncode("OK", System.Text.Encoding.UTF8)

        url = "www.google.com"
        req = HttpWebRequest.Create(url)
        req.Method = "POST"
        req.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR")
        req.Headers.Add(HttpRequestHeader.Cookie, cookie)
        req.Headers.Add(HttpRequestHeader.Pragma, "no-cache")
        req.Referer = "www.google.com"
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"
        req.AutomaticDecompression = DecompressionMethods.GZip
        req.ContentType = "application/x-www-form-urlencoded"
        req.ContentLength = PostData.Length

    End If

End If

Try

    Dim swRequestWriter As StreamWriter = New StreamWriter(req.GetRequestStream())

    swRequestWriter.Write(PostData)
    swRequestWriter.Close()

    Dim srResponseReader As New StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.Default)

    Html = srResponseReader.ReadToEnd()

    swRequestWriter.Close()

    srResponseReader.Close()


Catch ex As Exception
    Throw ex
End Try
  • I saw you never voted for anything. Did you know that in addition to accepting an answer in your question, you can also vote on the questions and answers of the entire site?

1 answer

2


That way it’s not closed. If an error occurs the closing command is never executed, it leaves the resource open and sooner or later will cause serious problems. The correct thing is to use the commando Using to ensure that the closure of resources implementing the disposable pattern always occur (see more).

Never call the Close() or the Dispose() on its own (unless you need it and know very well what you’re doing). This causes resource leaks when there are errors.

Anyway you should probably fix the mistakes. One of the reasons you’re not fixing is that you’re making another mistake. You are capturing the exception to do NOTHING. If you don’t have something useful to do there don’t capture the exception let it propagate to another place where something useful can be done. And if you still have nothing useful to do let the application break. And fix the error. Most exceptions are programming errors that must be fixed and not captured.

And before you say you’re doing anything when you catch an exception, throw ex; is not something useful, it just destroyed the call stack. If that were the case, it should just do throw;, but it would still be a mistake to have only this line. This command is only useful coming after doing something useful. Alone it is a mistake. Take this exception "treatment" and read about the subject at tag . Read it even, everything, it is necessary to learn to use it right. If you have specific questions, ask, but don’t misuse the feature.

Hiding the exception doesn’t make the code work right.

Documentation of Using. You should use at least 3 of them, maybe more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.