The underlying connection was closed / HttpWebRequest

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'm 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.br)
        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 anyone give me an orientation of what I could do to get around this problem?

Att.

    
asked by anonymous 15.10.2015 / 15:10

0 answers