问题向Ariba Supplier Network发布MIME编码附件

时间:2010-02-10 21:27:31

标签: vb.net post

我遇到了尝试使用VB.NET和WebRequest对象发布一些cXML文本和附件的问题。我相信我正在格式化我希望正确发布的数据文本,因为它匹配Ariba提供的样本。我不确定的是我是否正确使用WebRequest对象进行发布。我将Method设置为“POST”,将ContentType设置为“multipart / mixed”。我可以成功发布cXML(使用ContentType为“text / xml”),所以我知道在一般作品中发帖。我从Ariba回来的消息是“文档无法解析:缺少起始边界[ANCLS-741552]”,但是,如下面的代码所示,有一个起始边界。我删除了一些专有数据,并将其替换为“......”。在MIME文本之后,我已经包含了我正在使用的相关VB.NET代码。

欢迎并赞赏任何和所有建议!

------=_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">
<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)

3 个答案:

答案 0 :(得分:1)

你是对的,比尔。我做了以下更改并立即开始工作:

更改了一行代码

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

答案 1 :(得分:1)

比尔 - 我从Ariba那里得到了一些信息,并认为这对你有用:

  • 整个有效负载的Content-type声明指定边界
  • 每个部分前面都有一个' - '后跟边界
  • 文档终止时带有“ - ”后跟边界,后跟“ - ”
  • 每个部分的Content-ID都包含在尖括号
  • 附件的Content-ID在注释/附件中没有尖括号的网址元素中引用

也许您可以尝试使用缩短的边界字符串。另外,请注意'start'参数。这是适合我的那个:

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

具有上述边界和范围的示例XML。开始设置如下所示:

--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">

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

... 

<Comments>These also use bluetooth<Attachment><URL>cid:xx@cxml.org</URL></Attachment></Comments>

...

</Request></cXML>

--outerboundary1

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

Content-Disposition: attachment; filename=PremiereCatalog.xxx

Content-Length: 4

Content-ID: <xx@cxml.org>

xxxx

--outerboundary1-- 

我知道在那里得到很少的帮助会非常令人沮丧。所以lemme知道你是否需要完整的代码对我有用。

普拉

答案 2 :(得分:0)

我在将带有MIME内容的XML发布到Ariba SN时遇到了类似的错误。在没有MIME的情况下发布XML效果很好。

我得到的错误信息是: 无法解析该文档:prolog中不允许使用内容。 (第1行,第1栏)[ANCLS-741552]

我尝试上传的XML是:

------=_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">
<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--
????????????????????????????????????????????????????????????????????????????????????????????
.
.
????????????????????????????????????????????????????????????????????????????????????????????

我使用的VB.Net代码是:

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