0

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

Pettanord
  • 81
  • 1
  • 9
  • 1
    Possible duplicate of [Upload files with HTTPWebrequest (multipart/form-data)](https://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data) – Nico Schertler Nov 07 '18 at 11:45
  • Data in the format `application/x-www-form-urlencoded` is separated using ampersands, thus to merely add more parameters you don't have to change anything other than the string you pass to the function: `param1=value1&param2=value2`. Regarding file uploading see the duplicate above for a couple of possible solutions. – Visual Vincent Nov 07 '18 at 16:26

0 Answers0