【问题标题】:Attachments using REST WebService and VB.NET使用 REST WebService 和 VB.NET 的附件
【发布时间】:2011-05-26 15:41:04
【问题描述】:

我目前正在使用 VB.NET 开发一个应用程序,其中我正在使用 REST WebServices。我已经能够使用 REST 完成基本操作,但是,我无法添加附件(更具体地说,使用附加的 REST 上传文件)。我在网上进行了广泛的研究,但到目前为止,我还没有在 VB.NET 中找到任何工作示例。要实际上传数据,我使用 System.Net.WebClient。下面的 VB.NET 代码完成了重要的工作:

Dim Client As New System.Net.WebClient
Dim postBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(postString)
Client.UploadData(URL, "POST", postBytes)

我的网址的简化版本如下: "../REST/1.0/ticket/" + ticketNumber + "/comment?user=" + userName + "&pass=" + password

最后,我发布的内容的一个例子是:

postString = "content=Text: RT Test" + vbLf + "Action: Comment" + vbLf + "Attachment: examplefile.jpg" + vbLf + "attachment_1="

如您所见,postString 被转换为字节,然后上传到服务器。但是,我不知道应该在哪里或如何发布原始附件本身。我们专门使用状态来使用变量“attachment_1”的服务的文档,我将其添加到 postString 变量中,但我不确定下一步应该是什么。是否应该将文件转换为字节并附加到 postBytes 变量?我尝试了类似的操作,但收到一条错误消息,提示找不到 examplefile.jpg 的附件。

感谢您的帮助!

【问题讨论】:

  • 你试过Base64编码数据吗? Convert.ToBase64String(bytes)
  • 感谢您的建议。似乎对我不起作用,但是,您实际上是如何添加附件的?我们正在通过 REST 接口,所以在 URL 的末尾我们去:“URL\content=... Attachment: filename.xtension Action: comment .... attachment_1=" + Convert.ToBase64String(FileAsBytes(the file) ) 其中 FileAsBytes() 是将附件文件转换为字节的函数,但是,响应返回为:“......错误请求......”,不记得部分的确切措辞,因为现在不工作,任何建议或更精确的细节?谢谢。

标签: vb.net rest attachment rt


【解决方案1】:

我们不能使用 Client.UploadData(...) 并且必须将整个帖子转换为字节,从附件之前的 POST 字段开始,然后是附件本身,最后是 POST 字段的其余部分。

Public Sub AddAttachmentToRT(ByVal url As String, ByVal fileName As String, ByVal filePath As String)

    Dim dataBoundary As String = "--xYzZY"
    Dim request As HttpWebRequest
    Dim fileType As String = "image/jpeg" 'Will want to extract this to make it more generic from the uploaded file.

    'Create a POST web request to the REST interface using the passed URL
    request = CType(WebRequest.Create(url), HttpWebRequest)
    request.ContentType = "multipart/form-data; boundary=xYzZY"
    request.Method = "POST"
    request.KeepAlive = True

    'Write the request to the requestStream
    Using requestStream As IO.Stream = request.GetRequestStream()

        'Create a variable "attachment_1" in the POST, specify the file name and file type
        Dim preAttachment As String = dataBoundary + vbCrLf _
        + "Content-Disposition: form-data; name=""attachment_1""; filename=""" + fileName + """" + vbCrLf _
        + "Content-Type: " + fileType + vbCrLf _
        + vbCrLf

        'Convert this preAttachment string to bytes
        Dim preAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(preAttachment)

        'Write this preAttachment string to the stream
        requestStream.Write(preAttachmentBytes, 0, preAttachmentBytes.Length)

        'Write the file as bytes to the stream by passing its exact location
        Using fileStream As New IO.FileStream(Server.MapPath(filePath + fileName), IO.FileMode.Open, IO.FileAccess.Read)

            Dim buffer(4096) As Byte
            Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)

            Do While (bytesRead > 0)

                requestStream.Write(buffer, 0, bytesRead)
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)

            Loop

        End Using

        'Create a variable named content in the POST, specify the attachment name and comment text
        Dim postAttachment As String = vbCrLf _
        + dataBoundary + vbCrLf _
        + "Content-Disposition: form-data; name=""content""" + vbCrLf _
        + vbCrLf _
        + "Action: comment" + vbLf _
        + "Attachment: " + fileName + vbCrLf _
        + "Text: Some description" + vbCrLf _
        + vbCrLf _
        + "--xYzZY--"

        'Convert postAttachment string to bytes
        Dim postAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postAttachment)

        'Write the postAttachment string to the stream
        requestStream.Write(postAttachmentBytes, 0, postAttachmentBytes.Length)

    End Using

    Dim response As Net.WebResponse = Nothing

    'Get the response from our REST request to RT
    'Required to capture response, without this Try-Catch attaching will fail
    Try
        response = request.GetResponse()

        Using responseStream As IO.Stream = response.GetResponseStream()

            Using responseReader As New IO.StreamReader(responseStream)

                Dim responseText = responseReader.ReadToEnd()

            End Using

        End Using

    Catch exception As Net.WebException

        response = exception.Response

        If (response IsNot Nothing) Then

            Using reader As New IO.StreamReader(response.GetResponseStream())

                Dim responseText = reader.ReadToEnd()

            End Using

            response.Close()

        End If

    Finally

        request = Nothing

    End Try

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多