Need help with VB.NET command-line program to upload a file via HTTP (input type=file

timgt

Member
Joined
Mar 14, 2006
Messages
6
Programming Experience
Beginner
Hello,

I'm trying to write a vb.net command-line application that will upload files to a partner's website via a HTTP form. The problem I am having is that I have no idea how to do a HTTP POST where the input type = file (file to be uploaded). I can do a HTTP POST with all the other variables, just not the file uploading (input type=file).

Could somebody please tell me what I am doing wrong and/or need to change? Thanks!!!!

Here is the URL that contains the basic HTML to post and upload a file: http://66.228.120.89/temp/temp.html

Here is the code I have so far:

VB.NET:
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Namespace Examples.System.Net
Public Class WebRequestGetExample
Public Shared Sub Main()
Dim var1, var2, var3, var4, var5, var6 As String
Dim filename As String
Dim URL_topost As String
URL_topost = "http://www1.badongo.com/index.php?page=upload&"
var1 = "desc=gt_vid&"
var2 = "toc=1&"
var3 = "mature=NO&"
var4 = "UL_ID=801799385.1486042519.1141984650&"
var5 = "filename=c:\tow.jpg"
' Create a request for the URL. 
Dim request As WebRequest = WebRequest.Create(URL_topost & var1 & var2 & var3 & var4 & var5)
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
' Display the status.
Console.WriteLine(response.StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Cleanup the streams and the response.
reader.Close()
dataStream.Close()
response.Close()
End Sub 'Main
End Class 'WebRequestGetExample
End Namespace
 
System.Net.WebClient has got UploadFile methods too
 
I see the example.. thanks!

I still don't see how to actually use that. So lets say my URL is http://asf.pl?var=asdf&var2=asdf and then i have a type=file with name of var3

So how do I modify the code to use the var3 as the variable with the file to be uploaded to?

Thx in advance!
 
So what I am really getting at is this: The server has a form which says "type=file name=filename".

So when you are building the URI request, how exactly do you do it to include the variable names from above to stream your file to? The below does not work... please help!

Dim
uriString As String = "http://server.com/upload.pl?filename="
Dim responseArray As Byte() = myWebClient.UploadFile(uriString, "POST", fileName)

 
Essentially, the page I need to upload to is "blah.pl" and the server's form has "type=file name=filename" and I don't see anywhere where I can specify the variable name of "filename".

Help plz!
 
Back
Top