在请求中找不到边界

时间:2014-05-08 17:06:44

标签: docusignapi

我收到错误

  

解析多部分请求时发现错误。在请求中找不到边界终结符“--BOUNDARY--”。

我尝试使用Firefox和chrome进行调试,结果相同。 任何帮助将不胜感激。

以下是我正在使用的代码:

Public Sub configureMultiPartFormDataRequest(ByVal request As HttpWebRequest, _
                                                 ByVal xmlBody As String, _
                                                 ByVal docName As String)

        'Overwrite the default content-type header and set a boundary marker
        request.ContentType = "multipart/form-data; boundary=BOUNDARY"

        'Start building the multipart request body
        Dim requestBodyStart As String = "\r\n\r\n--BOUNDARY\r\n" + _
            "Content-Type: application/xml\r\n" + _
            "Content-Disposition: form-data\r\n" + _
            "\r\n" + _
            xmlBody + "\r\n\r\n--BOUNDARY\r\n" + _
            "Content-Type: application/pdf\r\n" + _
            "Content-Disposition: file; filename=\" + docName + " \  documentId=1\r\n" + _
            "\r\n"
        Dim requestBodyEnd As String = "\r\n--BOUNDARY--\r\n\r\n"

        'Read the contents of provided document into the request stream
        Dim fileStream As FileStream = File.OpenRead(docName)

        'Write the body of the request
        Dim bodyStart() As Byte = System.Text.Encoding.UTF8.GetBytes(requestBodyStart.ToString())
        Dim bodyEnd() As Byte = System.Text.Encoding.UTF8.GetBytes(requestBodyEnd.ToString())
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(bodyStart, 0, requestBodyStart.ToString().Length)

        'Read the file contents and write them to the request stream. (4096 Byte Blocks)
        Dim buf(4096) As Byte
        'Dim len As Integer
        'While ((len = fileStream.Read(buf, 0, 4096)) > 0)
        '    dataStream.Write(buf, 0, len)
        'End While
        Dim bytesRead As Integer
        Do
            bytesRead = fileStream.Read(buf, 0, buf.Length)
            If bytesRead > 0 Then
                dataStream.Write(buf, 0, bytesRead)
            End If
        Loop While bytesRead > 0


        dataStream.Write(bodyEnd, 0, requestBodyEnd.ToString().Length)


        dataStream.Close()

    End Sub

已添加05/09/2014 这是Fiddler2的实际请求文本。似乎bodyEnd被设置为“PDF-1.7”。我搜索了这个字符串的代码没有成功。感谢您的帮助:\ r \ n \ r \ n - BOUNDARY \ r \ nContent-Type:application / xml \ r \ nContent-Disposition:form-data \ r \ n \ r \ nndsentDocuSign API - Embedded Signing example1 \ 10.1.11.100 \ SecureDocs \ EnrollmentForms \ CrystalReport1.pdf1hmitchell@ata.eduA Adams10010011 \ r \ n \ r \ n - BOUNDARY \ r \ nContent-Type:application / pdf \ r \ nContent-Disposition:file;文件名= \ XXX \; documentId = 1 \ r \ n \ r \ n%PDF-1.7

大卫 感谢您修复BOUNDARY错误。我实现了你的xml示例,现在得到一个错误:“文档元素不包含编码文档,或者编码存在问题。” 这是请求: - 边界 Content-Type:application / xml 内容处理:表单数据

sentDocuSign API - 嵌入式签名example1C:\ Hold \ myXML.xml1hmitchell@ata.eduA Adams10010011

- 边界 内容类型:application / pdf 内容处理:文件; filename = \ C:\ Hold \ myXML.xml \ documentId = 1

    <nameValue>
        <name>canManageAccount</name>
        <value>false</value>
    </nameValue>

- 边界 -

感谢您的帮助!!!

2 个答案:

答案 0 :(得分:1)

上面的代码片段缺少调用方法,所以我自己创建了如下所示的代码片段,以演示如何利用WebHookapp端点来捕获正在发送的内容。因此,我认为您会看到正文格式不正确,因为您尝试&#34;转义&#34; 换行符和回车符号vs使用具有正确值的chr。

代码我添加了上面的方法

Imports System.IO
Imports System.Net

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim myHTTPRequest As System.Net.HttpWebRequest = WebRequest.Create("http://webhookapp.com/1037371688646089664")
    Dim myXML As String = "{SomeXMLorJason: sampleNotFormatedcorrectly}"
    Dim myDocName As String = "c:\myXML.xml"
    myHTTPRequest.Method = "POST"

    configureMultiPartFormDataRequest(myHTTPRequest, myXML, myDocName)


    myHTTPRequest.GetResponse()

End Sub

'Your Code goes here ---> from above:

End Class

文件c:\ myXML.xml

中的内容
        <nameValue>
            <name>canManageAccount</name>
            <value>false</value>
        </nameValue>

发送了什么:

POST /1037371688646089664
content-type    multipart/form-data; boundary=BOUNDARY
host    webhookapp.com
content-length  439
expect  100-continue
connection  Keep-Alive
\r\n\r\n--BOUNDARY\r\nContent-Type: application/xml\r\nContent-Disposition: form-data\r\n\r\n{SomeXMLorJason: sampleNotFormatedcorrectly}\r\n\r\n--BOUNDARY\r\nContent-Type: application/pdf\r\nContent-Disposition: file; filename=\c:\myXML.xml \  documentId=1\r\n\r\n                <nameValue>
                    <name>canManageAccount</name>
                    <value>false</value>
                </nameValue>\r\n--BOUNDARY--\r\n\r\n

如何修复代码:

 'Start building the multipart request body
        ' http://www.asciitable.com/

        Dim asciLN As String = Chr(10)
        Dim asciCR As String = Chr(13)

        Dim requestBodyStart As String = asciCR + asciLN + asciCR + asciLN + "--BOUNDARY" + asciCR + asciLN + _
            "Content-Type: application/xml" + asciCR + asciLN + _
            "Content-Disposition: form-data" + asciCR + asciLN + _
            asciCR + asciLN + _
            xmlBody + asciCR + asciLN + asciCR + asciLN + "--BOUNDARY" + asciCR + asciLN + _
            "Content-Type: application/pdf" + asciCR + asciLN + _
            "Content-Disposition: file; filename=\" + docName + " \  documentId=1" + asciCR + asciLN + _
            asciCR + asciLN
        Dim requestBodyEnd As String = asciCR + asciLN + "--BOUNDARY--" + asciCR + asciLN + asciCR + asciLN

会产生:

POST /1037371688646089664
content-type    multipart/form-data; boundary=BOUNDARY
host    webhookapp.com
content-length  409
expect  100-continue
connection  Keep-Alive

--BOUNDARY
Content-Type: application/xml
Content-Disposition: form-data

{SomeXMLorJason: sampleNotFormatedcorrectly}

--BOUNDARY
Content-Type: application/pdf
Content-Disposition: file; filename=\c:\myXML.xml \  documentId=1

                <nameValue>
                    <name>canManageAccount</name>
                    <value>false</value>
                </nameValue>
--BOUNDARY--

JSON中作为Multipart的一般信封应如下所示:

POST http://{server}/restapi/{apiVersion}/accounts/{accountId}/envelopes

X-DocuSign-Authentication: <DocuSignCredentials><Username>{name}</Username><Password>{password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>
Accept: application/json
Content-Type: multipart/form-data; boundary=AAA

--AAA
Content-Type: application/json
Content-Disposition: form-data
{
  "status":"sent",
  "emailBlurb":"Test Email Body",
  "emailSubject": "Test Email Subject - EnvelopeDefFull",
  "documents": [{
      "name": "test1.pdf",
      "documentId":"1"
      "order":"1"
  }],
  "recipients": {
    "signers" : [{
      "email": "test@email.com",
      "name": "Sally Doe",
      "recipientId":"1",
    }]
  }
}
--AAA
Content-Type: application/pdf
Content-Disposition: file; filename="test1.pdf";documentid=1

<document bytes removed>

--AAA--

答案 1 :(得分:-1)

一般来说,您的请求应如下所示。唯一的区别是此示例使用JSON格式而不是XML。只需更改您需要更改内容类型的application/jsonapplication/xml,然后将JSON数据更改为XML格式。

POST http://{server}/restapi/{apiVersion}/accounts/{accountId}/envelopes

X-DocuSign-Authentication: <DocuSignCredentials><Username>{name}</Username><Password>{password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>
Accept: application/json
Content-Type: multipart/form-data; boundary=AAA

--AAA
Content-Type: application/json
Content-Disposition: form-data
{
  "status":"sent",
  "emailBlurb":"Test Email Body",
  "emailSubject": "Test Email Subject - EnvelopeDefFull",
  "documents": [{
      "name": "test1.pdf",
      "documentId":"1"
      "order":"1"
  }],
  "recipients": {
    "signers" : [{
      "email": "test@email.com",
      "name": "Sally Doe",
      "recipientId":"1",
    }]
  }
}
--AAA
Content-Type: application/pdf
Content-Disposition: file; filename="test1.pdf";documentid=1

<document bytes removed>

--AAA--

如果没有看到代码正在构建和发送的实际请求,我只能假设格式化有问题。确保在需要的地方有额外的换行符(CRLF),并且正确地将文档字节写入请求。如果你仍然卡住了,你可能想要通过Fiddler这样的工具来检查请求数据......