Skip to content

Instantly share code, notes, and snippets.

@PyramisDev
Created July 30, 2013 14:23
Show Gist options
  • Save PyramisDev/6113351 to your computer and use it in GitHub Desktop.
Save PyramisDev/6113351 to your computer and use it in GitHub Desktop.
Transfers files using HTTP
'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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment