Peter Nordberg
asked on
Webrequest with form data and file
Hi,
I need to upload a file with a webrequest and combine it with some other form data in the request. Today my post request looks like this:
The string post (in request.ContentLength) now contains this form-data:
Now I want also to add this form data parameters:
file (a pdf file to upload)
recipient_email (string value)
recipient_name (string value)
How do I need to alter the PostData function to be able to post both the file and the string data in the webrequest?
Thanks for help!
Peter
I need to upload a file with a webrequest and combine it with some other form data in the request. Today my post request looks like this:
Protected Function PostData(ByRef url As String, ByRef POST As String, ByRef Cookie As System.Net.CookieContainer) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse
request = CType(WebRequest.Create(url), HttpWebRequest)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = POST.Length
request.Method = "POST"
request.AllowAutoRedirect = False
Dim requestStream As Stream = request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
response = CType(request.GetResponse, HttpWebResponse)
Return New StreamReader(response.GetResponseStream()).ReadToEnd()
End Function
The string post (in request.ContentLength) now contains this form-data:
"access_token=xxyyzzz"
Now I want also to add this form data parameters:
file (a pdf file to upload)
recipient_email (string value)
recipient_name (string value)
How do I need to alter the PostData function to be able to post both the file and the string data in the webrequest?
Thanks for help!
Peter
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
ASKER
Peter