2014-12-07 66 views
-1

我想知道這個「POST」請求是幹什麼的,並且將代碼翻譯成vb.net,以用於webrequest。會評論我所知道的已經Javascript to VB.Net [FEW LINES]

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行。

感謝您的幫助!

+2

請分享整個錯誤消息 – OneFineDay 2014-12-07 00:23:43

回答

0

當你做一個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() 

responseData將包含響應字符串

您也可以用一個try/catch將其包圍檢查是否有任何異常...互聯網連接不好,網址不正確,無反應等。xmlhttp.readyState == 4 && xmlhttp.status == 200在html中表示request finishedOK

+0

謝謝,正是我需要的。 – 2014-12-07 11:24:23