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