Search code examples
vb.nethttpwebrequestsystem.net.webexception

404 error while getting server response vb.net


I'm a totally beginner with webrequest, so I have no idea about what cause the error I get.

I try to login on a form following the microsoft tutorial for webrequest, but when I want to get the server response, I have the following error :

"the remote server returned an error (404) not found"

So I know that the URL I use actually exist and then wonder which part of the code is bad. Maybe it's because I'm doing an HTTPS request unlike the tutorial and it changes something ? Also, I'm a little confused by getting directly the answer from the server : shouldn't there be kind of a trigger to know when the server answered ?

Dim request = WebRequest.Create("https://ssl.vocabell.com/mytica2/login")
request.Credentials = CredentialCache.DefaultCredentials
request.Method = "POST"
Dim byteArray = Encoding.UTF8.GetBytes("_username=x&_password=x")
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
Dim dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim reponse = request.GetResponse() 'ERROR
MsgBox(CType(reponse, HttpWebResponse).StatusDescription)
Using ds = reponse.GetResponseStream
     Dim reader = New StreamReader(ds)
     MsgBox(reader.ReadToEnd)
End Using
reponse.Close()

Thank you for your time, and if you have any relevant tutorial on the topic I would be glad to read it !


Solution

  • The page you've mentioned does exist and uses HTTPS, but if you look at the form tag within it, it's like this:

    <form class="login-form form-horizontal" action="/mytica2/login_check" method="POST">
    

    This means it doesn't post the form back to the same URL as the page, instead it sends it to the URL contained within that "action" attribute. If you're trying to use your code to simulate the submission of the login form then it looks like you need to send your POST request to https://ssl.vocabell.com/mytica2/login_check instead.