Webclient UploadFile

bcorbett

Active member
Joined
Oct 16, 2006
Messages
27
Programming Experience
Beginner
I have following form to automate in vb.net:

VB.NET:
<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>
After I submit the form, I should recieve an XML string similar to this:

VB.NET:
<?xml version="1.0" ?> 
  <dmsXML version="1.0">
  <jobid value="0" /> 
  <rescode value="FAIL" /> 
  <rescode_description value="Submitted Job File Corrupt/Incomplete." /> 
  </dmsXML>

The following code doesn't give an error, but I still have no way of knowing if the file I'm trying to upload has made it or not. Is there a way to view the returned xml? The value of the string variable "response" is only "60" I'm not sure what that means. Am I even doing this whole thing right?:

VB.NET:
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)



Thanks for any Help!!!
 
WHAT I WANTED TO SEE!!! Thanks Dude!

VB.NET:
<?xml version="1.0"?>
BUBBA01
<dmsXML version="1.0"><jobid value="0"/>
    <rescode value="BADLOGIN"/>
    <rescode_description value="Authentication Failure."/>
</dmsXML>
I get the Badlogin error because this code is sending the login info as
Querystrings, its expecting POST Data from Inputs on the form.

I can use Webclient.uploadValues to do that , but how do I do the File upload in the same POST?
 
You have to create a "multipart/form-data" POST message (search to find examples), there one here also:
http://www.vbdotnetforums.com/net-sockets/25413-httpwebrequest-php-upload.html
There will be one "form-data; name=" section separated by boundary for each field.

You can install Fiddler Web Debugger - A free web debugging tool and make a post with the IE browser, then look at the textview request that was made from Inspectors tab, it's the same post message that is created here. (click 'view in Notepad' if there is binary data to see last fields)
Here is a sample multipart upload form that also can be used for testing in case yours is not easily browser accessible: Sample File Upload Form
 
First of all, thanks for the post. I think I have better understanding of this.
I did download fiddlier2 and I ran a test page that works and this is what I the post looked like:

VB.NET:
-----------------------------7d92bfe90ea0
Content-Disposition: form-data; name="MAX_FILE_SIZE"

5000000
-----------------------------7d92bfe90ea0
Content-Disposition: form-data; name="AUTH_NAME"

betatester
-----------------------------7d92bfe90ea0
Content-Disposition: form-data; name="AUTH_PW"

bl@ckG0ld!
-----------------------------7d92bfe90ea0
Content-Disposition: form-data; name="userfile"; filename="L:\degg.zip"
Content-Type: application/x-zip-compressed

PK

I created this code based on the example you sent me:

VB.NET:
Dim filepath As String = IO.Path.Combine(Application.StartupPath, "L:\degg.zip")
        Dim url As String = "http://www.somedomain.com/ims.php"

        Dim boundary As String = IO.Path.GetRandomFileName
        Dim header As New System.Text.StringBuilder()
        header.AppendLine("--" & boundary)
        header.Append("Content-Disposition: form-data; name=" & Chr(34) & "AUTH_NAME" & Chr(34) & "," & Chr(34) & "betatester" & Chr(34) & ";")
        header.Append("Content-Disposition: form-data; name=" & Chr(34) & "AUTH_PW" & Chr(34) & "," & Chr(34) & "bl@ckG0ld!" & Chr(34) & ";")
        'header.Append("Content-Disposition: form-data; name=""AUTH_NAME", "bl@ckG0ld!";")

        header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath))
        header.AppendLine()
        header.AppendLine("Content-Type: application/octet-stream")
        header.AppendLine()

        Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
        Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)

        Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
        req.ContentType = "multipart/form-data; boundary=" & boundary
        req.ContentLength = headerbytes.Length + New IO.FileInfo(filepath).Length + endboundarybytes.Length
        req.Method = "POST"

        Dim s As IO.Stream = req.GetRequestStream
        s.Write(headerbytes, 0, headerbytes.Length)
        Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
        s.Write(filebytes, 0, filebytes.Length)
        s.Write(endboundarybytes, 0, endboundarybytes.Length)
        s.Close()

        Dim responce As WebResponse = req.GetResponse()
        Dim ss As Stream = responce.GetResponseStream
        Dim sr As StreamReader = New StreamReader(ss)
        Me.TextBox1.Text = sr.ReadToEnd.ToString

The post header for this code looked like this, what am I doing wrong?
Where is this "-----------------------------7d92bfe90ea0" Comign from and it looks like there is 2 LFCR between the field name and value.:

VB.NET:
--oukfj0dc.0wb
Content-Disposition: form-data; name="AUTH_NAME","betatester";Content-Disposition: form-data; name="AUTH_PW","bl@ckG0ld!";filename="degg.zip"
Content-Type: application/octet-stream

PK


You have to create a "multipart/form-data" POST message (search to find examples), there one here also:
http://www.vbdotnetforums.com/net-sockets/25413-httpwebrequest-php-upload.html
There will be one "form-data; name=" section separated by boundary for each field.

You can install Fiddler Web Debugger - A free web debugging tool and make a post with the IE browser, then look at the textview request that was made from Inspectors tab, it's the same post message that is created here. (click 'view in Notepad' if there is binary data to see last fields)
Here is a sample multipart upload form that also can be used for testing in case yours is not easily browser accessible: Sample File Upload Form
 
The post header for this code looked like this, what am I doing wrong?
You haven't formatted the message correctly, it should look similar to the first quote. Use header.ToString to see the message as you build it.
Where is this "-----------------------------7d92bfe90ea0" Comign from
it is "--" followed by the boundary string, you should be able to see how this is used in the code sample. You can read some theory about this in these and related RFCs:
RFC 2388 (rfc2388) - Returning Values from Forms: multipart/form-data
RFC 2046 (rfc2046) - Multipurpose Internet Mail Extensions (MIME) Part Two
 
Back
Top