tags:

views:

266

answers:

4

I'm having an issue with trying to use VB.NET and the WebRequest object to post some cXML text and an attachment. I believe I am formatting the text of the data I wish to post correctly, as it matches samples Ariba has supplied. What I am uncertain of is if I am correctly using the WebRequest object to do the posting. I am setting the Method as "POST" and the ContentType as "multipart/mixed". I can successfully post cXML (using a ContentType of "text/xml"), so I know posting in general works. The message I get back from Ariba is "The document could not be parsed: Missing start boundary [ANCLS-741552]", however, as the following code shows, there is a start boundary. I have removed some proprietary data and replaced it with "...". Following the MIME text, I have included the relevant VB.NET code I am using.

Any and all suggestions are most welcome and appreciated!

------=_boundary.20100210154837663.173702
Content-Type: text/xml; charset=UTF-8
Content-ID: Part1.5213.20100210154837663.319349

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.018/cXML.dtd"&gt;
<cXML.../cXML>

------=_boundary.20100210154837663.173702
Content-Type: application/octet-stream
Content-transfer-encoding: base64
Content-ID: 5213.20100210154837663.319349
Content-Disposition: attachment; filename=attachment_20100210154837663_125705.jpg
Content-Length: 5660

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAd
.
.
.
dC2Nz9dOaQ1ZVIilwIpaWvLEg7aWuauv/9k=
------=_boundary.20100210154837663.173702--


Dim myWriter As StreamWriter = Nothing

Dim strUri As String = "https://service.ariba.com/service/transaction/cxml.asp"

Dim WebReq As WebRequest = System.Net.WebRequest.Create(strUri)

WebReq.Method = "POST"
WebReq.ContentType = "multipart/mixed"

myWriter = New StreamWriter(WebReq.GetRequestStream())
myWriter.Write(xmlText)
myWriter.Close()

Dim WebRsp As HttpWebResponse
WebRsp = CType(WebReq.GetResponse(), HttpWebResponse)
A: 

I am facing a similar error in POSTing an XML with MIME content to Ariba SN. POSTing XML without MIME works well.

The error msg I am getting is: The document could not be parsed: Content is not allowed in prolog. (line 1, column 1) [ANCLS-741552]

The XML I am trying to upload is:

------=_Part_19_14056801.1265738936575
Content-Type: text/xml; charset=UTF-8
Content-Disposition: attachment; filename=TestMessage020910.xml
Content-ID:

?<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.021/InvoiceDetail.dtd"&gt;
<cXML>...</cXML>

------=_Part_19_14056801.1265738936575
Content-Type: image/tiff
Content-transfer-encoding: binary
Content-ID: TestImageURL
Content-Disposition: attachment; filename=TestImageURL.tif
Content-Length: 23618

II*? [??&?lDr#??292??292?`?????pr9?3]?2s7???L?@7?r9?A?a?A??h4?B?d;? r1?
.
.
????[?????????(???????1?7????[??2????.\??@????????????????%?? ????%?? ?Pixel Translations Inc., PIXTIFF Version 57.0.218.2183??2007:12:11 09:55:11?
------=_Part_19_14056801.1265738936575--
????????????????????????????????????????????????????????????????????????????????????????????
.
.
????????????????????????????????????????????????????????????????????????????????????????????

The VB.Net code I am using is:

Private Function Post_XML(ByVal XmlFile As String) As String
  If XmlFile.Length = 0 Then
   MsgBox("Error: XML File cannot be blank", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
   Return ""
  End If

  If File.Exists(XmlFile) = False Then
   MsgBox("Error: File does not exist - " + XmlFile, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
   Return ""
  End If

  Dim ASN_Link As String = "https://service.ariba.com/service/transaction/cxml.aspx"
  rtbLog.Text = ""
  System.Threading.Thread.Sleep(1000)

  rtbLog.Text = rtbLog.Text & vbCrLf & "To " & ASN_Link & vbCrLf

  Dim sr As StreamReader = New StreamReader(XmlFile)
  Dim XMLRead As String = sr.ReadToEnd
  Dim stXML As String
  stXML = XMLRead
  sr.Close()

  Try
   Dim results As String

   Dim request As WebRequest = WebRequest.Create(ASN_Link)
   request.Method = "POST"
   request.ContentType = "text/xml; charset=utf-8"

   Dim proxyObject As WebProxy = New WebProxy("proxy.xyz.com:8080", True)
   proxyObject.Credentials = CredentialCache.DefaultCredentials
   request.Proxy = proxyObject
   request.PreAuthenticate = True
   results = WriteToURL(request, stXML)

   Dim Response As String
   Response = RetrieveFromURL(request)
   rtbLog.Text = rtbLog.Text & vbCrLf & Response

   Return "OK"

  Catch ex As Exception
   MessageBox.Show(ex.Message, "", MessageBoxButtons.OK)
   Return "Error"
   Exit Function
  End Try

 End Function

  Private Function WriteToURL(ByVal Request As WebRequest, ByVal data As String) As String

    Try
   'Dim bytes = System.Text.Encoding.ASCII.GetBytes(data)
   Dim bytes = System.Text.Encoding.UTF8.GetBytes(data)
      Request.ContentLength = bytes.Length

      Dim OutPutStream As Stream = Request.GetRequestStream
      OutPutStream.Write(bytes, 0, bytes.length)
   OutPutStream.Close()
   Return "Ok"
    Catch ex As Exception
   MessageBox.Show(ex.Message & " - Write To URL", "error", MessageBoxButtons.OK)
   Return "Error"
      Exit Function
    End Try

  End Function

  Private Function RetrieveFromURL(ByVal Request As WebRequest) As String

    Try
      Dim response As WebResponse = Request.GetResponse
      Dim stream As Stream = response.GetResponseStream
      Dim sr As StreamReader = New StreamReader(stream)
      Return sr.ReadToEnd
    Catch ex As Exception
   MessageBox.Show(ex.Message & " - Retrieve From URL", "", MessageBoxButtons.OK)
   Return "Error"
      Exit Function
    End Try

  End Function
pravinsn1
One thing I do see (and I did receive that error when I had not yet made this change) is that the ContentType for a MIME document needs to be multipart/mixed or multipart/related, instead of text/xml. Or, at least, it should not be text/xml, but should, instead, be something else that I don't know of. The documentation for cXML seems pretty clear on that one part, though. Hope that helps, and thanks for posting. You never know where an idea for a solution can come from!
Bill
A: 

You are right, Bill. I made the below change and its working now:

Changed one line of code

from:
      request.ContentType = "text/xml; charset=utf-8"
to
      request.ContentType = "multipart/related;boundary=outerboundary1;type=""text/xml"";start=""<x.xml>"""
pravinsn1
A: 

Bill - I got some info from Ariba and thought it might be useful to you:

  • The Content-type declaration for the whole payload specifies the boundary
  • Each part is preceded w/ a '--' followed by the boundary
  • The document terminates with a '--' followed by boundary followed by '--'
  • The Content-ID of each part is enclosed in angle brackets
  • The Content-ID of the attachment is referenced in the URL element in Comments/Attachment w/o angle brackets

Maybe you could try with a shorted boundary string. Also, note the 'start' parameter. Here's the one that worked for me:

request.ContentType =  "multipart/related;boundary=outerboundary1;type=""text/xml"";start=""<x.xml>"""

A sample XML with the above boundary & start settings looks like this:

--outerboundary1

Content-Type: text/xml

Content-ID: <x.xml>

<?xml version="1.0"?>

<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.007/InvoiceDetail.dtd"&gt;

<cXML payloadID="za1saasxsx" timestamp="2003-04-04" xml:lang="en"><Header><From> 

... 

<Comments>These also use bluetooth<Attachment><URL>cid:[email protected]</URL></Attachment></Comments>

...

</Request></cXML>

--outerboundary1

Content-type: text/plain; charset=US-ASCII

Content-Disposition: attachment; filename=PremiereCatalog.xxx

Content-Length: 4

Content-ID: <[email protected]>

xxxx

--outerboundary1-- 

I know it can be very frustrating with very little help out there. So lemme know if you need the complete code that worked for me.

Pravin

pravinsn1
A: 

Pravinsn,

I am having similar difficulty and would love to see your code if it's still available.

Thanks!

Brad