The underlying Connection was closed / Httpwebrequest

Asked

Viewed 85 times

0

I use this request to get the return of a page, but my connection is rejected with this message The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. Follow the model of my connection below, knowing that the data is fictitious. I am using framework 4.0:

   PostData &= "&" & System.Web.HttpUtility.UrlEncode("TS25b9b7_ct", Encoding.UTF8) & "=" & System.Web.HttpUtility.UrlEncode(aux4, System.Text.Encoding.UTF8)
    PostData &= "&" & System.Web.HttpUtility.UrlEncode("TS25b9b7_pd", Encoding.UTF8) & "=" & System.Web.HttpUtility.UrlEncode(aux5, System.Text.Encoding.UTF8)

    PostData = PostData.Replace("%3a", "%3A").Replace("%2f", "%2F")

    Try
        System.Net.ServicePointManager.Expect100Continue = False

        req = HttpWebRequest.Create("http://www.google.com)
        req.Method = "POST"
        req.Headers.Add(HttpRequestHeader.AcceptLanguage, "pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3")
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:41.0) Gecko/20100101 Firefox/41.0"
        req.ContentLength = PostData.Length
        req.ContentType = "application/x-www-form-urlencoded"
        req.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
        req.Referer = "http://www.google.com"
        req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        req.KeepAlive = False

        Dim swRequestWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
        swRequestWriter.Write(PostData)
        swRequestWriter.Close()

        resp = req.GetResponse()
        Stream = resp.GetResponseStream()

        Dim reader2 As New StreamReader(Stream, System.Text.Encoding.UTF8)

        Html = reader2.ReadToEnd().Trim()

        resp.Close()
        Stream.Close()
        reader2.Close()


    Catch ex As WebException

        Dim webException = TryCast(ex, WebException)
        Dim response = DirectCast(webException.Response, HttpWebResponse)
    End Try

Could someone give me an orientation of what I could do to get around this problem?

Att.

  • 1

    Tried to change it? req.KeepAlive = True

  • I suggest testing whether the original HTTP endpoint responds correctly using a tool like [Fiddler][1] or [Postman][2]. [1]: http://www.telerik.com/fiddler [2]: https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomophl=en

  • First I used req.Keepalive = true, but I was not successful and after reading in some forums I was told to set to False. Now it didn’t help. The pair

  • Originally I used the req.KeepAlive = True, but I was unsuccessful. All parameters were extracted from Fiddler.

  • Everything was done using Fiddler.

No answers

Browser other questions tagged

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