I have tried a couple of different versions of this and keep coming up with "ReadToEnd is not a member of System.IO.Stream". Google can't seem to find anything with that exact error message (it keeps sending me to the official MSDN links on how to use ReadToEnd and System.IO.Stream) and keeps removing any quotes I put around the message, preventing me from searching only on that.
Here's the code as it currently stands:
Public Sub Main()
Dim FilePathName As String = Dts.Variables.Item("InboundPathAndName").Value
MsgBox("The Creds are: " + Dts.Variables.Item("HttpAuth").Value.ToString + " and the website is: " + Dts.Variables.Item("HttpPostURL").Value.ToString _
+ " and the file path / name is: " + Dts.Variables.Item("InboundPathAndName").Value.ToString)
Try
'create the information we need to send as part of post to let
'the ASPX page know about the file data
Dim DataString As StringBuilder = New StringBuilder
DataString.Append("------=_Part_153_2117173873.1044898432007" + vbCrLf)
DataString.Append("Content-Disposition: form-data; name=" + _
"""" + "file" + """" + "; filename=" + """" + FilePathName + """" + vbCrLf)
'set the file type to octet-stream so we can handle any kind of Data()
DataString.Append("Content-Type: application/octet-stream" + vbCrLf + vbCrLf)
Dim webReq As HttpWebRequest = SetWebRequest(New System.Uri(Dts.Variables.Item("HttpPostURL").Value), FilePathName)
'send the data about the file
SendFileInformation(webReq.GetRequestStream(), DataString, FilePathName)
'Get the response from the server
Dim webResp As HttpWebResponse = CType(webReq.GetResponse(), HttpWebResponse)
Dim sr As Stream = webResp.GetResponseStream()
Dim readStream As New StreamReader(sr, Encoding.UTF8)
'put the stream data in a string
Dim respData As String = sr.ReadToEnd()
sr.Close()
webResp.Close()
MessageBox.Show(respData)
Catch webExcp As WebException
'If you reach this point, an exception has been caught.
'Write out the WebException message.
MessageBox.Show("An error occured." + webExcp.ToString())
Return
Catch myExcp As Exception
MessageBox.Show("A WebException has been caught." + myExcp.ToString())
Return
End Try
Dts.TaskResult = ScriptResults.Success
End Sub
Private Function SetWebRequest(ByVal Url As System.Uri, ByVal FilePathName As String) As HttpWebRequest
Dim webReq As HttpWebRequest
webReq = CType(WebRequest.Create(Url), HttpWebRequest)
'POST Data
webReq.Method = "POST"
webReq.Headers.Add("Accept-Language", "en-us")
webReq.KeepAlive = True
' convert username:password to basic 64 format and append to the HTTP header
webReq.Headers.Add("Authorization", Dts.Variables.Item("HttpAuth").Value.ToString)
'If My.Computer.FileSystem.FileExists(FilePathName) Then
'webReq.ContentLength = New FileInfo(FilePathName).Length
'Else
' Throw New Exception("File not found")
' End If
webReq.ContentType = "multipart/form-data; boundary=------=_Part_153_2117173873.1044898432007"
webReq.Headers.Add("Accept-Encoding", "gzip, deflate")
Return webReq
End Function
Private Sub SendFileInformation(ByVal tempStream As Stream, ByVal DataString As StringBuilder, ByVal FilePathName As String)
Dim ReadIn As FileStream = New FileStream(FilePathName, FileMode.Open, FileAccess.Read)
ReadIn.Seek(0, SeekOrigin.Begin) 'move to the start of the file
Dim DataRead As Integer = 0
Dim FileData(1024) As Byte
Dim FileInfo As Byte() = System.Text.Encoding.Default.GetBytes(DataString.ToString())
tempStream.Write(FileInfo, 0, FileInfo.Length)
Do
DataRead = ReadIn.Read(FileData, 0, 1024)
If (DataRead > 0) Then 'we have data
tempStream.Write(FileData, 0, DataRead)
Array.Clear(FileData, 0, 1024) 'clear the array
End If
Loop While (DataRead > 0)
Dim endingBoundary As Byte() = _
System.Text.Encoding.Default.GetBytes(vbCrLf + "------=_Part_153_2117173873.1044898432007" + vbCrLf)
' send the closing boundry
tempStream.Write(endingBoundary, 0, endingBoundary.Length)
'close the stream
ReadIn.Close()
tempStream.Close()
End Sub
End Class
Any thoughts on why I'm getting this specific error?
I'm sure I need to change the code around the response (the String declaration probably won't work), but I've tried everything short of the Console.Writeline because I don't want this to write to the Console. Eventually the response is going to a SQL table as soon as I make sure the message box returns correctly. But before I get there, I need to figure out what is going on with this specific error.