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@do e.com&pass word=fooba r"
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-ur lencoded"
.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.Cooki es)
Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.G etResponse Stream)
Dim HTML As String = sReader.ReadToEnd
Debug.Write(HTML)
-------------------------- ---------- ---------- ---------- ---------- ---------- ------
i have also tried:
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Crea te("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.
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@do
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-ur
.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.Cooki
Dim sReader As IO.StreamReader = New IO.StreamReader(LoginRes.G
Dim HTML As String = sReader.ReadToEnd
Debug.Write(HTML)
--------------------------
i have also tried:
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Crea
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:\
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.
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.Crea te("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
Dim MyProxy As New System.Net.WebProxy("mypse rver", 80)
Dim MyCR As New System.Net.NetworkCredenti al("userna me", "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( ).GetStrin g(buffer))
stream.Close()
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.Crea
Dim MyProxy As New System.Net.WebProxy("mypse
Dim MyCR As New System.Net.NetworkCredenti
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(
stream.Close()
Try the following:
__________________________ __________ ____
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Crea te("http://admin.breezecentral.com/api/xml?action=login&login=jon@doe.com&password=foobar")
Dim MyCR As New System.Net.NetworkCredenti al("userna me", "password", "mydomain")
LoginReq.Proxy = System.Net.WebProxy.GetDef aultProxy( )
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( ).GetStrin g(buffer))
stream.Close()
__________________________ __________ ____
_______________
Nayer Naguib
__________________________
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Crea
Dim MyCR As New System.Net.NetworkCredenti
LoginReq.Proxy = System.Net.WebProxy.GetDef
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(
stream.Close()
__________________________
_______________
Nayer Naguib
Sorry! You can remove the line
Dim MyCR As New System.Net.NetworkCredenti al("userna me", "password", "mydomain")
from the above code.
_______________
Nayer Naguib
Dim MyCR As New System.Net.NetworkCredenti
from the above code.
_______________
Nayer Naguib
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put the following code in the form (or page) Load event handler:
__________________________
Dim LoginReq As System.Net.WebRequest = System.Net.WebRequest.Crea
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(
stream.Close()
__________________________
When you run the project, you should see the contents of the response (XML data) displayed in the textbox.
_______________
Nayer Naguib