Solved

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

Posted on 2004-10-11
3
202 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now