views:

14

answers:

0

Hello

According to this question, it seems like the error "Too many automatic redirections were attempted" is caused when forgetting to use a cookiecontainer to connect to a web server that uses cookies to keep track of the user.

However, even though I used "request.CookieContainer = MyCookieContainer", I'm still getting into an endless loop that is terminated by VB Express with this error message.

Imports System.IO
Imports System.Net
'Remember to add reference to System.Web DLL
Imports System.Web
Imports System.Text

Public Class Form1
    Const ConnectURL = "http://www.acme.com/logon.php"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim request As HttpWebRequest = WebRequest.Create(ConnectURL)

        'Build POST data
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"
        Dim Data As New StringBuilder
        Data.Append("Account=" + HttpUtility.UrlEncode("jdoe"))
        Data.Append("&Password=" + HttpUtility.UrlEncode("test"))
        Dim byteData() As Byte
        byteData = UTF8Encoding.UTF8.GetBytes(Data.ToString())
        request.ContentLength = byteData.Length
        Dim postStream As Stream = Nothing
        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        'Dim MyCookieContainer As New CookieContainer
        Dim MyCookieContainer As CookieContainer = New CookieContainer()
        request.CookieContainer = MyCookieContainer


        'Makes no difference
        'request.KeepAlive = True
        'request.AllowAutoRedirect = True

        Dim response As HttpWebResponse
        'HERE
        '"Too many automatic redirections were attempted"
        response = request.GetResponse()

        Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
        RichTextBox1.Text = reader.ReadToEnd

    End Sub
End Class

This is probably a newbie issue, but I don't know what else to try. Any idea?

Thank you for any hint.


Edit: Turns out the web server was sending a 302/Redirect without filling the Location bit:

HTTP/1.1 302 Found
Location: