Link to home
Start Free TrialLog in
Avatar of FlexQ
FlexQ

asked on

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.
Avatar of nayernaguib
nayernaguib
Flag of Egypt image

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
Avatar of FlexQ
FlexQ

ASKER

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()
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
Sorry! You can remove the line

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

from the above code.

_______________

  Nayer Naguib
Avatar of FlexQ

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial