VB   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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 = httpR_233_11845@ethod.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.

大佬总结

以上是大佬教程为你收集整理的vb.net – 将多部分表单发布到Bamboo API全部内容,希望文章能够帮你解决vb.net – 将多部分表单发布到Bamboo API所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。