vb.net – 将多部分表单发布到Bamboo API

前端之家收集整理的这篇文章主要介绍了vb.net – 将多部分表单发布到Bamboo API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
通过VB.NET控制台应用程序将多部分表单提交给BambooHR API有很多困难.我发布了我当前的代码以及下面的文档中的示例请求,当我运行这个代码(400)Bad Request.我知道代码是凌乱的,但我一直在试图让它工作.

我能够通过使用他们的示例代码来做出GET请求,但是他们没有任何代码来执行此特定的API调用(上传员工文件).

任何帮助将不胜感激.

这是我的代码

Sub Main()

    upload(id,"https://api.bamboohr.com/api/gateway.PHP/company")

    Console.WriteLine()
    Console.WriteLine("Press ENTER to quit")
    Console.ReadLine()
End Sub

Function upload(ByVal employeeId As Integer,ByVal baseUrl As String)

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3

    Dim boundary = "----BambooHR-MultiPart-Mime-Boundary----"
    Dim url = String.Format("{0}/v1/employees/{1}/files/",baseUrl,employeeId)

    Dim request As HttpWebRequest = WebRequest.Create(url)
    request.KeepAlive = True
    request.Method = "POST"
    request.ContentType = "multipart/form-data; boundary=" + boundary

    'Authorization is just the api key and a random string,in this case is x
    '
    Dim authInfo As String = api_key + ":" + "x"
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
    request.Headers("Authorization") = "Basic " + authInfo


    Dim memStream As New MemoryStream()

    WriteMPF(memStream)

    request.ContentLength = memStream.Length

    Using requestStream = request.GetRequestStream()
        memStream.Position = 0
        Dim tempBuffer As Byte() = New Byte(memStream.Length - 1) {}
        memStream.Read(tempBuffer,tempBuffer.Length)
        memStream.Close()
        requestStream.Write(tempBuffer,tempBuffer.Length)
    End Using

    Dim webresponse As HttpWebResponse = request.GetResponse()
    Return webresponse

End Function

Private Sub WriteMPF(s As Stream)

    WriteToStream(s,"POST /api/gateway.PHP/company/v1/employees/id/files/ HTTP/1.0")
    WriteToStream(s,vbCr & vbLf)
    WriteToStream(s,"Host: api.bamboohr.com")
    WriteToStream(s,"Content-Type: multipart/form-data; boundary=----BambooHR-MultiPart-Mime-Boundary----")
    WriteToStream(s,"Content-Length: 520")
    WriteToStream(s,vbCr & vbLf)

    WriteToStream(s,"------BambooHR-MultiPart-Mime-Boundary----")
    WriteToStream(s,"Content-Disposition: form-data; name=""category""")
    WriteToStream(s,"14")
    WriteToStream(s,"Content-Disposition: form-data; name=""fileName""")
    WriteToStream(s,"test.txt")
    WriteToStream(s,"Content-Disposition: form-data; name=""share""")
    WriteToStream(s,"no")
    WriteToStream(s,"Content-Disposition: form-data; name=""file""; filename = ""test.txt""")
    WriteToStream(s,"Content-Type: text/plain")
    WriteToStream(s,"this is a test!")
    WriteToStream(s,"------BambooHR-MultiPart-Mime-Boundary------")
    WriteToStream(s,vbCr & vbLf)
End Sub

Private Sub WriteToStream(s As Stream,txt As String)
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(txt)
    s.Write(bytes,bytes.Length)
End Sub

以下是文档的示例请求:(链接https://www.bamboohr.com/api/documentation/employees.php向下滚动到“上传员工文件”)

POST /api/gateway.PHP/sample/v1/employees/1/files/ HTTP / 1.0
主持人:api.bamboohr.com
Content-Type:multipart / form-data;边界= —- BambooHR-多部分MIME-边界—-
内容长度:520

—— BambooHR-多部分MIME-边界—-
内容处理:表单数据; NAME = “类别”

112
—— BambooHR-多部分MIME-边界—-
内容处理:表单数据; NAME = “文件名”

readme.txt文件
—— BambooHR-多部分MIME-边界—-
内容处理:表单数据; NAME = “分享


—— BambooHR-多部分MIME-边界—-
内容处理:表单数据; NAME = “文件”;文件名= “README.TXT”
Content-Type:text / plain

这是一个示例文本文件.

—— BambooHR-多部分MIME-边界——

我怀疑至少你的Content-Length:520会出错.内容长度只适用于他们的例子.

无论如何,我没有在很长一段时间内编写VB.Net,但是从一个快速测试中,这段代码修改版本对我的一个REST服务起作用,所以它应该在你的情况下工作,或许有一些微小的调整.

我的测试控制台项目使用了.Net 4.6.1,但可能会运行一些早期的.Net框架.

Imports System.IO
Imports System.Net.Http

Module Module1

    Sub Main()
        Call UploadFileToWebsite(14,"no","D:\Temp\file.pdf")
        Console.WriteLine("Please wait for a response from the server and then press a key to continue.")
        Console.ReadKey()
    End Sub

    Public Sub UploadFileToWebsite(category As Integer,share As String,file As String)
        Dim message = New HttpRequestMessage()
        Dim content = New MultipartFormDataContent()

        content.Add(New StringContent(category.ToString()),"category")
        content.Add(New StringContent(share),"share")

        Dim filestream = New FileStream(file,FileMode.Open)
        Dim fileName = System.IO.Path.GetFileName(file)

        content.Add(New StreamContent(filestream),"file",fileName)

        message.Method = HttpMethod.Post
        message.Content = content
        message.RequestUri = New Uri("https://api.bamboohr.com/api/gateway.PHP/company")

        Dim client = New HttpClient()
        client.SendAsync(message).ContinueWith(
            Sub(task)
                'do something with response
                If task.Result.IsSuccessStatusCode Then
                    Console.WriteLine("Uploaded OK.")
                Else
                    Console.WriteLine("Upload Failed.")
                End If
            End Sub)
    End Sub
End Module

在不相关的笔记中,您还可以使用vbCrLf而不是vbCr& vbLf.

原文链接:https://www.f2er.com/vb/255192.html

猜你在找的VB相关文章