By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

How to upload file via HttpWebRequest POST?

P: n/a
I'd like to upload a file in vis POST using HttpWebRequest. It needs
to simulate some one using a webform. The webform works fine when I
manually upload through the web browser. However, this code will return an
error 500 on the line indicated below. I'm passing the upload file field
name and value basically through a URL. Any suggested how I can get this
file to upload?

Dim MainForm As New InterfaceMain
Dim strResult As String = ""
Dim strPost As String = "method=upLoadList"
strPost = strPost & "&FileToUpload=c:\mytextfile.txt"

Dim objRequest As HttpWebRequest =
Dim myWriter As StreamWriter

objRequest.Method = "POST"
objRequest.ContentLength = strPost.Length
objRequest.ContentType = "multipart/form-data"

myWriter = New StreamWriter(objRequest.GetRequestStream())
Catch ex As Exception
End Try


Dim objResponse As System.Net.HttpWebResponse =
objRequest.GetResponse 'Internal error 500 occurs here
Dim sr As New System.IO.StreamReader(objResponse.GetResponseStre am)
Dim result As String = sr.ReadToEnd

I've also tried appended "?" to the URL
but that doesn't work either.

Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.