[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1379
  • Last Modified:

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.
0
FlexQ
Asked:
FlexQ
  • 3
  • 2
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now