Upload file via invoke code

I am trying to upload file via invoke code (VB) but getting bad request. Do we have to make any changes to define file content? Below is the code:

Dim fr As System.Net.HttpWebRequest
Dim targetURI As New Uri(strURL)
Dim postStream As Stream

fr = DirectCast(HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
Dim webProxy As IWebProxy = New WebProxy(strIpAddressAndPort)
webProxy.Credentials = New NetworkCredential(strProxyUsername, strProxyPassword)
fr.Proxy = webProxy

fr.Headers.Add(“Authorization”, jwtOutput)
fr.Headers.Add(“zapiAccessKey”, “MWExM2QyZTYtYzBlMi0zODI5LWIzMjUtZWNjZDJjMDBkODQ5IDYyMjhlODliYzg4ZjEwMDA2ODMyNWRhNCBVU0VSX0RFRk”)

fr.Method= “POST”
fr.ContentType = “multipart/form-data”

Dim bytes As Byte() = System.Text.Encoding.UTF8.GetBytes(FilePath)

postStream = fr.GetRequestStream()
postStream.Write(bytes,0,bytes.Length)
postStream.Close()

Dim res As HttpWebResponse = DirectCast(fr.GetResponse(), System.Net.HttpWebResponse)
webRes = res
Dim html As String = New StreamReader(res.GetResponseStream()).ReadToEnd()
strHtml = html

@pushkar.singh

I guess you need to keep the request alive using fr.keepalive = true

Cheers

Hi,

It doesn’t seem correct as multipart/form-data. For example, lack of information for boundary in ContentType or there is no Content-Disposition in Request Body etc.

https://www.ietf.org/rfc/rfc7578.txt

Perhaps, you should search and check sample of multipart/form-data for VB.net.

Regards,

Same results after adding fr.keepalive = true line.

vb.net - Upload file using httpwebrequest with same session - Stack Overflow

Worked this code for me with proxy

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.