Javascript to VB.Net [FEW LINES]

Javascript to VB.Net [FEW LINES]

本文关键字:FEW LINES Net to VB Javascript      更新时间:2023-09-26

我试图找出这个"POST"请求的作用,并将代码翻译成 vb.net,用于网络请求。将评论我已经知道的内容

var xmlhttp; //Defining the "xmlhttp" variable
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest() 
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
    }
    xmlhttp.onreadystatechange = function() { //If the xmlhttp is created then do this:
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { //Don't know
            fired = true; //The function gathered the conditions to start
            var title = '<div id="message_box_title_unavailable">woops!</div>'; //Doesn't matter
            var result; //Variable "result"
            if (xmlhttp.responseText == 'available') { //If the xml file contains "available" on it then do this, else return an error...
                result = 'The name you selected <strong>' + user + 'is available!'
            } else {
                result = 'There was an error processing your request.  Please try again.'
            }
        }
    };
    xmlhttp.open("POST", "Checkusername.php", true); //Didn't I translate this correctly?
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //What am I missing from this line in my vb code?
    xmlhttp.send("name=" + name + "&n=" + Math.random()) //The same...
}

好的 - 我的 Vb 翻译

Dim req As HttpWebRequest
Dim res As HttpWebResponse
Dim cookies As New CookieContainer()
req = WebRequest.Create("http://blablabla.net/Checkusername.php?name=" & name "&n=" R.Next(0, 20000))
req.Method = "POST"
req.ContentType = "application/xml"
req.Headers.Add("Content-type", "application/x-www-form-urlencoded")
req.CookieContainer = cookies
res = req.GetResponse
Dim webStream As Stream
webStream = res.GetResponseStream
Dim reader As New StreamReader(webStream)
checksource.text = reader.readtoend

总是向我返回一个"错误"而不是所需的结果,这在 HTML 中总是正确的

基本上,我无法翻译第 3/5 行和最后 3 行。

感谢您帮助大家!

当你做一个 GET 时,你在 url 中提供参数。当您执行 POST 时,您可以在请求中提供它们。此外,如果您不使用它,不确定为什么要创建 cookie 对象。

您不需要:

Dim res As HttpWebResponse
Dim cookies As New CookieContainer()

试试这个:

'Create the post string which contains the params
Dim postString As String = "name=" & name & "&n=" & R.Next(0, 20000)
'Create the request
req = WebRequest.Create("http://blablabla.net/Checkusername.php")
req.Method = "POST"
req.ContentLength = postString.Length
req.ContentType = "application/x-www-form-urlencoded"
'put the POST params in the request
Dim requestWriter As StreamWriter = New StreamWriter(req.GetRequestStream())
requestWriter.Write(postString)
requestWriter.Close()
'submit the request and read the response
Dim responseReader As StreamReader = New StreamReader(req.GetResponse().GetResponseStream())
Dim responseData As String = responseReader.ReadToEnd()
'close everything
responseReader.Close()
req.GetResponse().Close()
响应

数据将包含您的响应字符串

您还可以用 Try/Catch 包围它以检查是否有任何异常......互联网连接不良,网址错误,无响应等。 html 中的xmlhttp.readyState == 4 && xmlhttp.status == 200表示request finishedOK