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 —— BambooHR-多部分MIME-边界—- 112 readme.txt文件 是 这是一个示例文本文件. —— 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:Tempfile.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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |