vb.Net http post

摘要:vb.Net http post


刚刚看到一段简短的程序是使用vb.net 开发应用程序来对网站使用POST的方式传送数据

Dim web As New System.Net.WebClient()
web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes("SEARCHSTRING=test")
Dim res As Byte() = web.UploadData("http://somewhere.com/search.asp", "POST", d)
msgbox(System.Text.Encoding.ASCII.GetString(res))

比起下面这种方法是简单许多!

Dim httpResp As System.Net.HttpWebResponse
Dim httpUrl2 As New System.Uri("http://somewhere.com/search.asp")
Dim req As HttpWebRequest
req = CType(WebRequest.Create(httpUrl2), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim bytesData() As Byte = Encoding.ASCII.GetBytes("SEARCHSTRING=test")
req.ContentLength = bytesData.Length
Dim postStream As Stream = req.GetRequestStream()
postStream.Write(bytesData, 0, bytesData.Length)
postStream.Close()
Dim res As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)
Dim reader As StreamReader = New StreamReader(res.GetResponseStream,System.Text.Encoding.GetEncoding("BIG5"))
Dim respHTML As String = reader.ReadToEnd()
res.Close()
MsgBox(respHTML)