WebClient.uploadfile and WebClient.uploadValues


  • I have the following HTML Form That I'm trying to automate:
    <!-- The data encoding type, enctype, MUST be specified as below -->
    <form enctype="multipart/form-data"
    action="" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
    <input type=”hidden” name=”AUTH_NAME” value=”john” />
    <input type=”hidden” name=”AUTH_PW” value=”mypass” />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
    I can use WebClient.UploadFile to upload the file and I can use Webclient.uploadValues to pass the Form Fields "AUTH_NAME" and "AUTH_PW".

    But, my question is how to do i do both in the same POST?

    I tried this, but the remote page doesn't work with query strings:

    Dim RemotePath As String = ""
            Dim InsertClient As New WebClient()
            InsertClient.QueryString.Add("AUTH_NAME", "username")
            InsertClient.QueryString.Add("AUTH_PW", "password")
            Dim Response As Byte() = InsertClient.UploadFile (RemotePath, "POST", "L:\")

    I've also tried using the Webclient.Credentials property with no luck.
    On another thread they suggested usign the WebClient.GetWebRequest method but, I have no idea where to start.

    Any help would be appreciated!
    Thursday, July 23, 2009 1:42 PM

All replies

  • Hi,

    Take a look at the following answer:

    The code is in C#, but you could use a utility like for instance this to convert it to VB.Net code

    Thursday, July 23, 2009 8:25 PM
  • I get the following erro when I try to convert to VB:

    Statement fragment: please enter a complete statement  .

    I did come up with this to upload the file, now I need to know how to add the two Form Field Values (AUTH_NAME and AUTH_PW). I think I can just tac it onto the stream, but I'm not sure of where:


    Sub Upload(ByVal uploadUrl As String)


    Dim rdr As New FileStream("L:\", FileMode.Open)


    Dim req As HttpWebRequest = DirectCast(WebRequest.Create(uploadUrl), HttpWebRequest)

    req.Method =


    req.ContentLength = rdr.Length

    req.AllowWriteStreamBuffering =



    Dim reqStream As Stream = req.GetRequestStream()


    Dim inData As Byte() = New Byte(1023) {}


    Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)


    While bytesRead > 0

    reqStream.Write(inData, 0, bytesRead)

    bytesRead = rdr.Read(inData, 0, inData.Length)


    End While




    Dim responce As WebResponse = req.GetResponse()


    Dim s As Stream = responce.GetResponseStream


    Dim sr As StreamReader = New StreamReader(s)


    Me.TextBox1.Text = sr.ReadToEnd.ToString



    End Sub

    Friday, July 24, 2009 1:07 PM