使用HttpClient for WEB API在VB.Net中进行上传



我想通过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)

最新更新