Javascript到VB.Net[几行]

Javascript到VB.Net[几行],javascript,.net,vb.net,Javascript,.net,Vb.net,我试图找出这个“POST”请求的作用,并将代码翻译成vb.net,用于webrequest。我会评论我已经知道的 var xmlhttp; //Defining the "xmlhttp" variable if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest() } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")

我试图找出这个“POST”请求的作用,并将代码翻译成vb.net,用于webrequest。我会评论我已经知道的

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...
}
这总是返回一个“错误”,而不是期望的结果,在HTML中总是正确的

基本上,我不能翻译第3/5行和最后3行


谢谢你们的帮助

执行GET时,在url中提供参数。当你写一篇文章时,你会在请求中提供它们。此外,如果不使用cookies对象,则不确定为什么要创建它

您不需要:

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来检查任何异常…错误的internet连接、错误的url、无响应等。
xmlhttp.readyState==4&&xmlhttp.status==200
在html中表示
请求已完成
确定

请共享整个错误消息谢谢,这正是我需要的。