VB.net 2005 向服务器POST信息(上传文件等)

前端之家收集整理的这篇文章主要介绍了VB.net 2005 向服务器POST信息(上传文件等)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


'上传文件
Public Function uploadTheFile(ByVal uploadUrl As String,ByVal uploadFileName As String) As String
Dim HostUrl As New System.Uri(uploadUrl)
Dim request As System.Net.HttpWebRequest
Dim fs As New System.IO.FileStream(uploadFileName,FileMode.Open,FileAccess.Read) '得到本地文件的数据流

request = CType(System.Net.WebRequest.Create(HostUrl),System.Net.HttpWebRequest)
request.Timeout = 1000 * 5 * 60 '设置超时值30秒
request.Method = "POST" '传送方式
request.KeepAlive = True
request.ContentLength = fs.Length '设置传送的数据长度

Dim PostStream As Stream = request.GetRequestStream() '发送请求
Dim b(1024) As Byte
Dim n As Integer
'向服务器写入文件
n = fs.Read(b,b.Length)
Do While n > 0
PostStream.Write(b,n)
n = fs.Read(b,b.Length)
Loop
fs.Flush() '清空缓冲区
fs.Close()
'以上向服务器 POST 信息。

'以下是获取服务器返回信息
Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(),System.Net.HttpWebResponse)
Dim ReStream As StreamReader = New StreamReader(response.GetResponseStream,

System.Text.Encoding.GetEncoding("GB2312"))

Dim resStr As String = ReStream.ReadToEnd() MsgBox(response.StatusCode.ToString) '向网络服务器 POST 后返回的状态码,成功则返回"OK" response.Close() '关闭 End Fu

猜你在找的VB相关文章