Windows Form upload data file by HTTP POST

J

Jason Ho

Hi,

I would like to send a jpg file to a HTTP server by POST request. But I am
not using a browser to do this, I use a Windows Form instead. I know how to
send typical request by POST with code below. But I don't know how to encode
the data file so the server can be recognized.

1 ) Traditional HTML code for upload a file:
<form action="http://abc.com/save_photos.php" name="upload_form"
enctype="multipart/form-data" method="POST">

2 ) My vb.net code for POST request:
Dim lcUrl As String = "http://abc.com/save_photos.php"
Dim myHttpWebRequest As HttpWebRequest =
DirectCast(WebRequest.Create(lcUrl), HttpWebRequest)
myHttpWebRequest.Method = "POST"

' Create a new string object to POST data to the Url.
Dim inputData As String = "cmd=" & HttpUtility.UrlEncode("login")

Dim postData As String = inputData
Dim encoding As New ASCIIEncoding
Dim byte1 As Byte() = encoding.GetBytes(postData)

' Set the content type of the data being posted.
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

'Do I need to set cotnet type as below if transfer data file?
'myHttpWebRequest.ContentType = "multipart/form-data encode"


' Set the content length of the string being posted.
myHttpWebRequest.ContentLength = postData.Length
Dim newStream As Stream = myHttpWebRequest.GetRequestStream()
newStream.Write(byte1, 0, byte1.Length)
newStream.Close()

Hope you can help.

Thanks,
Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top