VB.NET C/S Upload Files to Server 暂未测试通过

1.dim  HostURL as new System.URI(Http://127.0.0.1/c:/..../)

   dim request as HttpWebRequest

   dim fs as New FileStream(filePath,FileMode.Open,FileAccess.Read)

 

request = CType(WebRequest.Create(HostURL),HttpWebRequest)

request.timeout = 1000*5*60

request.Method="POST"

request.KeepAlive=True

request.ContentLengh= fs.length

 

dim postStream as Stream = request.GetRequestStream()

dim strData as String

 

dim sr as StreamReader = New StreamReader(path,false)

dim swSaveTo as StreamWriter = New StreamWriter(path,false)

 

dim by(1024) as Byte

dim n as Integer

n = fs.Read(by,0,by.length)

strData = sr.ReadLine()

 

Do While n>0

  swSaveTo.WriteLine(strData,postStream)

  n = fs.Read(by,0,by.Length)

Loop

 

fs.flush()

fs.Close()

sr.Close()

swSaveTo.Close()

 

 

----------------------------------------

 

 '上传文件
    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, 0, b.Length)
        Do While n > 0
            PostStream.Write(b, 0, n)
            n = fs.Read(b, 0, 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


本文来自CSDN博客:http://blog.csdn.net/ITshu/archive/2009/02/27/3943149.aspx

 

 

 

你可能感兴趣的:(VB.NET)