Solved

Uploading a file to a perl script in VB.NET using POST

Posted on 2004-10-11
3
205 Views
Last Modified: 2012-06-21
I have the folloing HTML form that works pefectly well uploading files to a perl script

<html><B>File Uploader</B><p></p>
<form  name='frm' enctype='multipart/form-data' action='https://www.myurl.com/cgi-bin/upload.pl?p1=111&p2=adestindationfilename.jpg&p3=xxxx&cmd=PUT' method='POST'>    
<input name='lclfile' type='file' value=''>
<input name='Submit' type='submit' value='Upload'>
 </form>      
</html>


I want to duplicate the HTML functionality above in a .NET assembly (fully trusted) so that the file selection and the POST occurs automatically. The file is ALWAYS a binary image and the parameters to the perl script are mandatory.

I tried to following code to simulate the Submit on a normal SSL Web form which fails to upload. Any ideas why?
The error occurs at System.NET.ConnectStream.get_Length() - Not supported Exception
PS. I have control of the perl script too.

                  pURL = "https://www.myurl.com/cgi-bin/upload.pl"

                    'Store any credential cookies in container
                    'Create the Web Request
                    strParamData = "p1=111&p2=adestinationfilename.jpg&p3=xxxx&cmd=PUT"
                    Dim PUTRequest As HttpWebRequest = HttpWebRequest.Create(pURL & "?" & strParamData)
                    'Give default credentials to the PutRequest - 'normaly this would be from a CredentailCache
                    PUTRequest.Credentials = CredentialCache.DefaultCredentials
                   
                    Dim myBoundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
                    PUTRequest.ContentType = "multipart/form-data; boundary=" & myBoundary
                    PUTRequest.Method = "POST"

                    'Now Setup the POST Message
                    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
                    sb.Append("--")
                    sb.Append(myBoundary)
                    sb.Append("\r\n")
                    sb.Append("Content-Disposition: " & "form-data; name=""lclfile""" & "; filename=""")
                    sb.Append(_mylocalfile)
                    sb.Append("""")
                    sb.Append("\r\n")
                    sb.Append("Content-Type: application/octet-stream")
                    sb.Append("\r\n")
                    sb.Append("\r\n")

                    'Put the contents of the sting into a header
                    Dim myHeader As String = sb.ToString
           
                    Dim myHeaderBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(myHeader)

                    'Now build the tailing boundary
                    Dim myBoundaryBytes As Byte() = System.Text.Encoding.UTF8.GetBytes("\r\n" & myBoundary & "\r\n")

                    'Get the file into a Stream
                    Try
                        If System.IO.File.Exists(_mylocalfile) Then
   
                            Dim myFileStream As New FileStream(_mylocalfile FileMode.Open, FileAccess.Read)
                            Dim myFileLen As Integer
                            myFileLen = myFileStream.Length
                            Dim filebuffer(myFileLen) As Byte

                            myFileStream.Read(filebuffer, 0, filebuffer.Length)
                            myFileStream.Close()



                            'Total Length of POST
                            Dim myTotalPOSTLength As Long = myHeaderBytes.Length + filebuffer.Length + myBoundaryBytes.Length
                            PUTRequest.ContentLength = myTotalPOSTLength
                            Dim myReqStream As Stream = PUTRequest.GetRequestStream

                            myReqStream.Write(myHeaderBytes, 0, myHeaderBytes.Length)
                            'Now write out the file contents
                            myReqStream.Write(filebuffer, 0, filebuffer.Length)
                            'Now write out the tailing boundary
                            myReqStream.Write(myBoundaryBytes, 0, myBoundaryBytes.Length)
                            'Now we get the Response back
                            Dim myResponse As WebResponse = PUTRequest.GetResponse
                        End If

                    Catch ex As Exception
                        MsgBox("Error Occurred: " & ex.Message)
                    End Try




0
Comment
Question by:westerdale
3 Comments
 
LVL 1

Author Comment

by:westerdale
ID: 12347656
Note to Administrator: I've solved this issue myself...Can I give myself 500 points <g>

just  PUTRequest.AllowWriteStreamBuffering = True was missing!

Regards,

Simon
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12663305
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.NET tools for adding thread safety to a web app? 3 48
VB 2005 Tooltips on Form Load Event 15 34
MYSQL responding very slow 3 23
Access Schema 6 26
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question