-1

I am a complete newbie in VB.NET but I am trying to implement the following curl command in VB.NET code:

curl -u <userName>:<password> -XPOST -F 'installer=@OnHoliday.wav' 'https://192.168.1.51?archiveSpace=1'

Can anybody point me in the right direction? Should I use a httpwebrequest, webclient, httpclient? I have been pounding my head against a wall for the last two days but I can't seem to get it to work.

Thanks.

Tony Dong
  • 3,213
  • 1
  • 29
  • 32
Ted B
  • 21
  • 5
  • 2
    Possible duplicate of [Curl command to html or vb.net](http://stackoverflow.com/questions/14835916/curl-command-to-html-or-vb-net) – Juliën Feb 23 '17 at 19:33
  • I have been searching here and other places for two days now. I saw that post but I can't the file to load. This is a multipart form with the file. I saw a good article using a webclient but that defaults to "file=@thefile" where I need "installer=@thefile". I could not find a way to change it. – Ted B Feb 23 '17 at 21:48

1 Answers1

1

Let me know how this .Net 4.5 console app works for you...

Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Threading.Tasks

Module Module1

    Private Credentials As New NetworkCredential("userName", "password")
    Private Client As New HttpClient(New HttpClientHandler With {.PreAuthenticate = True, .Credentials = Credentials})

    Private Const FormFieldName As String = "installer"
    Private Const Filename As String = "OnHoliday.wav"
    Private Const ContentType As String = "audio/x-wav"
    Private Const Url As String = "https://192.168.1.51?archiveSpace=1"

    Sub Main()
        With DoPost()
            .Wait()
            Debug.Print(.Result)
        End With
    End Sub

    Async Function DoPost() As Task(Of String)
        Dim FileContent = New ByteArrayContent(File.ReadAllBytes(Filename))
        FileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType)

        Dim RequestContent = New MultipartFormDataContent(Guid.NewGuid.ToString)
        RequestContent.Add(FileContent, FormFieldName, Filename)

        With Await Client.PostAsync(Url, RequestContent)
            Return Await .Content.ReadAsStringAsync
        End With
    End Function

End Module

References:

MrGadget
  • 1,258
  • 1
  • 10
  • 19