加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net http post的两种方法

发布时间:2020-12-17 07:40:37 所属栏目:百科 来源:网络整理
导读:剛剛看到一段簡短的程式是使用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("

剛剛看到一段簡短的程式是使用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,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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读