获取服务器响应vb.net时出现404错误

时间:2019-07-01 10:33:12

标签: vb.net http webrequest system.net.webexception

我是webrequest的初学者,所以我不知道导致错误的原因。

我尝试按照Microsoft教程针对Webrequest登录表单,但是当我想获取服务器响应时,出现以下错误:

  

“远程服务器返回未找到错误(404)”

所以我知道我使用的URL实际上存在,然后想知道代码的哪一部分不好。也许是因为与教程不同,我正在执行HTTPS请求,并且它有所更改? 另外,直接从服务器获取答案也让我有些困惑:是否应该有某种触发器来知道服务器何时响应?

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()

谢谢您的时间,如果您有关于该主题的任何相关教程,我将很高兴阅读它!

1 个答案:

答案 0 :(得分:1)

您提到的页面确实存在并且使用HTTPS,但是如果您查看其中的form标记,则就像这样:

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

这意味着它不会将表单发布回与页面相同的URL,而是将其发送到该“ action”属性中包含的URL。如果您尝试使用代码来模拟登录表单的提交,那么您似乎需要将POST请求发送到https://ssl.vocabell.com/mytica2/login_check