【问题标题】:Curl form/file upload in VB.NETVB.NET中的卷曲表单/文件上传
【发布时间】:2017-07-14 10:47:10
【问题描述】:

我是 VB.NET 的新手,但我正在尝试在 VB.NET 代码中实现以下 curl 命令:

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

谁能指出我正确的方向?我应该使用 httpwebrequest、webclient、httpclient 吗?过去两天我一直在用头撞墙,但似乎无法正常工作。

谢谢。

【问题讨论】:

  • 我已经在这里和其他地方搜索了两天了。我看到了那个帖子,但我无法加载文件。这是一个包含文件的多部分表单。我看到一篇使用网络客户端的好文章,但默认为“file=@thefile”,我需要“installer=@thefile”。我找不到改变它的方法。

标签: vb.net curl


【解决方案1】:

让我知道这个.Net 4.5 控制台应用程序如何为您工作...

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

参考资料:

【讨论】:

    猜你喜欢
    • 2011-06-07
    • 2020-04-24
    • 2012-06-21
    • 2013-03-21
    • 2018-01-17
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    相关资源
    最近更新 更多