使用HTTP传输文件

vbscript
阅读 81 收藏 0 点赞 0 评论 0

HTTP File Transfer.vbs
'Does not have to be hardcoded like below, can be pulled from a text box or something
Dim strPostURL = "http://www.google.com"

        AddText("URL TO POST: " + strPostURL)

        Dim requestStream As Stream = Nothing
        Dim fileStream As FileStream = Nothing
        Dim uploadResponse As Net.HttpWebResponse = Nothing

        Try

            Dim uploadRequest As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(strPostURL), Net.HttpWebRequest)
            uploadRequest.Method = Net.WebRequestMethods.Http.Post
            ' UploadFile is not supported through an Http proxy
            ' so we disable the proxy for this request.
            uploadRequest.Proxy = Nothing

            requestStream = uploadRequest.GetRequestStream()
            fileStream = File.Open("c:\temp\MyActions_Partial_Feed_Format.xml", FileMode.Open)

            Dim buffer(1024) As Byte
            Dim bytesRead As Integer
            While True
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                If bytesRead = 0 Then
                    Exit While
                End If
                requestStream.Write(buffer, 0, bytesRead)
            End While

            ' The request stream must be closed before getting the response.
            requestStream.Close()

            uploadResponse = uploadRequest.GetResponse()
            Dim responseReader As StreamReader = New StreamReader(uploadRequest.GetResponse.GetResponseStream())
            Dim x As String = responseReader.ReadToEnd()
            responseReader.Close()
            AddText(x)



        Catch ex As UriFormatException
            AddText(ex.Message)
        Catch ex As IOException
            AddText(ex.Message)
        Catch ex As Net.WebException
            AddText(ex.Message)
        Finally
            If uploadResponse IsNot Nothing Then
                uploadResponse.Close()
            End If
            If fileStream IsNot Nothing Then
                fileStream.Close()
            End If
            If requestStream IsNot Nothing Then
                requestStream.Close()
            End If
        End Try
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号