none
WebClient.uploadfile and WebClient.uploadValues

    Question

  • I have the following HTML Form That I'm trying to automate:
    Code:
    <html>
    <body>
    <!-- The data encoding type, enctype, MUST be specified as below -->
    <form enctype="multipart/form-data"
    action="http://www.nikonet.com/api/customerid.php" 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" />
    </form>
    </body>
    
    </html>
    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:

    Code:
    Dim RemotePath As String = "http://www.doamin.com/api/rigd_ims.php"
            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:\degg.zip")
            MsgBox(Response(0).ToString)

    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:

    Public

    Sub Upload(ByVal uploadUrl As String)

     

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

     

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

    req.Method =

    "PUT"

    req.ContentLength = rdr.Length

    req.AllowWriteStreamBuffering =

    True

     

    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

    rdr.Close()

    reqStream.Close()

     

    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