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 vb.net 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 =
WebRequest.Create("http://www.somesite.com")
Dim myWriter As StreamWriter

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

Try
myWriter = New StreamWriter(objRequest.GetRequestStream())
myWriter.Write(strPost)
Catch ex As Exception
MsgBox(ex.Message())
Finally
myWriter.Close()
End Try

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
sr.Close()

I've also tried appended "?" to the URL
http://www.somesite.com?
but that doesn't work either.

Thanks,
Brett
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.