簡體   English   中英

使用 HttpClient for WEB API 在 VB.Net 中上傳

[英]Upload in VB.Net using HttpClient for WEB API

我想通過 HTTPS 在 VB.NET 中使用 HttpClient 將文件上傳到供應商 WEB API。 我有這個代碼來上傳文件:

Dim httpclient As WebClient
Dim _cookieStore As Cookie
Dim HOST As String = "http://test.com/"
Dim USER_ID As String = "userid"
Dim PASSWORD As String = "password"
Dim FILE_ID As String = "DOC_00000000000000_0000000000"
Dim SPACE_ID As String = "SPACE_00000000000000_0000000000"
Dim FILE_PATH As String = "C:\" & vbTab & "est.txt"
Dim FILE_NAME As String = "test.txt"
Public SEND_CHUNK_SIZE As Long = (1024 * 1024)

Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    Dim dialogResult = openFileDialog.ShowDialog()
    If dialogResult <> dialogResult.OK Then Return
    upload(openFileDialog.FileName)
End Sub

Private Async Sub upload(ByVal filePath As String)
    Dim file As String = System.IO.Path.GetFileName(filePath)
    Dim readed As Long = 0
    Dim size As Long = 0
    Dim info As New FileInfo(file)
    Dim chunk As Long = 0
    Try
        readed = 0

        Dim filename = Path.GetFileName(file)
        Dim client As HttpClient = New HttpClient()
        Dim postData As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
        postData.Add(New KeyValuePair(Of String, String)("filename", filename))
        postData.Add(New KeyValuePair(Of String, String)("versionkind ", "1"))
        postData.Add(New KeyValuePair(Of String, String)("verUp ", "true"))
        postData.Add(New KeyValuePair(Of String, String)("chunk ", chunk))
        postData.Add(New KeyValuePair(Of String, String)("chunks ", "0"))
        Dim content As HttpContent = New FormUrlEncodedContent(postData)
        MsgBox(content)
        Dim response = Await client.PostAsync(HOST + "/contentsmanagement/ContentsAdd.json", content)
        response.EnsureSuccessStatusCode()

        chunk = (chunk + 1)            
    Catch e As Exception
        MsgBox(e.Message)
        Return
    End Try
End Sub

我正在努力使用 VB.Net,但我不知道如何上傳它。 我對 VB.Net 不是很好。 任何幫助表示贊賞。

我認為 HttpClient 對您的目的來說有點先進。

我建議改用 WebClient。 你可以這樣使用它:

Dim client As WebClient = New WebClient()
client.UploadFile(url, filename)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM