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://www.baidu.com", "POST", d)
msgbox(System.Text.Encoding.ASCII.GetString(res))

比起下面這種方法是簡單許多!

Dim httpResp As System.Net.HttpWebResponse
Dim httpUrl2 As New System.Uri("http://www.baidu.com")
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)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值