Solved

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

Posted on 2004-10-11
3
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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