HttpWebRequest - 400 Bad Request - VB.Net

I'm trying to use a HttpWebRequest to login to a Breeze server. I've tried a WebClient also. I keep getting a '400 bad request' reponse from the server.

This URL works fine through a browser:
http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar

Its a test server so go ahead. Play with it.

Here is my code:
Dim encoding As New System.Text.UTF8Encoding
        Dim PostData As String = "action=login&login=jon@doe.com&password=foobar"
        Dim Data() As Byte = encoding.GetBytes(PostData)
        Dim LoginReq As Net.HttpWebRequest = Net.WebRequest.Create("http://admin.breezecentral.com/api/xml")
        Dim CookieC As New Net.CookieContainer
       
        With LoginReq
            .KeepAlive = False
            .Method = "POST"
            .ContentType = "application/x-www-form-urlencoded"
            .ContentLength = Data.Length
            .CookieContainer = CookieC
        End With

        Dim SendReq As IO.Stream = LoginReq.GetRequestStream
        SendReq.Write(Data, 0, Data.Length)
        SendReq.Close()

        Dim LoginRes As Net.HttpWebResponse = LoginReq.GetResponse()
        CookieC.Add(LoginRes.Cookies)

        Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.GetResponseStream)
        Dim HTML As String = sReader.ReadToEnd
        Debug.Write(HTML)
----------------------------------------------------------------------------------
i have also tried:

        Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
        Dim LoginRes As System.Net.WebResponse = LoginReq.GetResponse()
        Dim stream As System.IO.Stream = LoginRes.GetResponseStream()
        Dim local As System.IO.Stream = System.IO.File.Create("E:\Documents and Settings\BenBlanco\Desktop\res.txt")

        Dim buffer(1023) As Byte
        Dim n As Integer
        Do
            n = stream.Read(buffer, 0, buffer.Length)
            local.Write(buffer, 0, n)
        Loop While n > 0

        stream.Close()
        local.Close()

Please help. I'm sure I have some small setting wrong.
FlexQAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nayernaguibCommented:
Add a textbox called TextBox1 to your form (whether a web form or a Windows form).
Put the following code in the form (or page) Load event handler:

_______________________________________________

        Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
        Dim LoginRes As System.Net.WebResponse = LoginReq.GetResponse()
        Dim stream As System.IO.Stream = LoginRes.GetResponseStream()
        Dim buffer(1023) As Byte
        Dim n As Integer

        Do
            n = stream.Read(buffer, 0, buffer.Length)
        Loop While n > 0

        TextBox1.Text = New System.Text.ASCIIEncoding().GetString(buffer)

        stream.Close()

_______________________________________________

When you run the project, you should see the contents of the response (XML data) displayed in the textbox.

_______________

  Nayer Naguib
0
FlexQAuthor Commented:
It didn't work for me.

I don't think it matters. but I'm using a proxy.

my complete code is:
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
        Dim MyProxy As New System.Net.WebProxy("mypserver", 80)
        Dim MyCR As New System.Net.NetworkCredential("username", "password", "mydomain")
        MyProxy.Credentials = MyCR
        LoginReq.Proxy = MyProxy
        Dim LoginRes As System.Net.WebResponse = LoginReq.GetResponse()
       
        Dim stream As System.IO.Stream = LoginRes.GetResponseStream()
        Dim buffer(1023) As Byte
        Dim n As Integer

        Do
            n = stream.Read(buffer, 0, buffer.Length)
        Loop While n > 0

        Debug.Write(New System.Text.ASCIIEncoding().GetString(buffer))

        stream.Close()
0
nayernaguibCommented:
Try the following:

________________________________________

        Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Create("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
        Dim MyCR As New System.Net.NetworkCredential("username", "password", "mydomain")
        LoginReq.Proxy = System.Net.WebProxy.GetDefaultProxy()
        Dim LoginRes As System.Net.WebResponse = LoginReq.GetResponse()
        Dim stream As System.IO.Stream = LoginRes.GetResponseStream()
        Dim buffer(1023) As Byte
        Dim n As Integer

        Do
            n = stream.Read(buffer, 0, buffer.Length)
        Loop While n > 0

        Debug.Write(New System.Text.ASCIIEncoding().GetString(buffer))

        stream.Close()

________________________________________

_______________

  Nayer Naguib
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nayernaguibCommented:
Sorry! You can remove the line

        Dim MyCR As New System.Net.NetworkCredential("username", "password", "mydomain")

from the above code.

_______________

  Nayer Naguib
0
FlexQAuthor Commented:
That GetDefaultProxy didn't work.

I fixed it. I'm going to kick myself now, though.

I just changed the port from 80 to 8080 and it worked. Thanks.
0
GranModCommented:
PAQed with points refunded (500)

GranMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.