如何使用Excel VBA发布附带IBM Connections的论坛主题

我正在尝试使用Excel VBA将带有附加映像文件的论坛主题发布到IBM Connections 5.0。 根据IBM Connections API描述,此处将需要多部分请求。

我已经pipe理的是发布一个论坛主题没有附件,并附加文本或图像文件到现有的维基页面。 因此,我假设问题与这些方面没有关系,而是与多部分请求的正确格式相关。 API描述在这里我不是很清楚,我在其他帮助论坛上尝试了几件有关多部分请求的内容。 但是,我所得到的只是一个回应“400错误的要求”。

也许你们有些专家可以给我一个关于我的代码的提示:

Public Sub CreateForumPost() Const sBoundary As String = "2588eb82-2e1c-4aec-9f4f-d65a3ecf8fab" Dim oHttp As MSXML2.xmlhttp Dim sUrl As String Dim sBody As String 'create XMLHTTP object and URL Set oHttp = CreateObject("MSXML2.XMLHTTP") sUrl = "https://my-connect-server/forums/atom/topics?forumUuid=9e51cbfb-4b1d-405d-9835-dbd087c49a65" 'create forum post sBody = "--" & sBoundary & vbCrLf sBody = sBody & "<?xml version=""1.0"" encoding=""UTF-8""?>" sBody = sBody & "<entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:app=""http://www.w3.org/2007/app"" xmlns:snx=""http://www.ibm.com/xmlns/prod/sn"">" sBody = sBody & "<category scheme=""http://www.ibm.com/xmlns/prod/sn/type"" term=""forum-topic""/>" sBody = sBody & "<title type=""text""> " & "My Title" & " </title>" sBody = sBody & "<category term=""question"" scheme=""http://www.ibm.com/xmlns/prod/sn/flags""/>" sBody = sBody & "<category term=""" & "my-tag" & """/>" sBody = sBody & "<content type=""html""> " & "My post content" & " </content>" sBody = sBody & "</entry>" & vbCrLf sBody = sBody & "--" & sBoundary & vbCrLf sBody = sBody & "Content-Disposition: attachment; filename=""dummy.txt""" & vbCrLf & vbCrLf sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf sBody = sBody & "--" & sBoundary & "--" & vbCrLf Call oHttp.Open("POST", sUrl, False) Call oHttp.setRequestHeader("Content-Type", "multipart/related;boundary=" & sBoundary & ";type=""application/atom+xml""") Call oHttp.send(pvToByteArray(sBody)) If oHttp.Status = 201 Then Call MsgBox("success") Else Call MsgBox("error") Stop End If End Sub Private Function sGetFile(sName As String) As String Dim abyContent() As Byte Dim iNumber As Integer Dim lLen As Long lLen = FileLen(sName) If lLen > 0 Then ReDim abyContent(lLen - 1) iNumber = FreeFile Open sName For Binary Access Read As iNumber Get iNumber, , abyContent Close iNumber sGetFile = StrConv(abyContent, vbUnicode) Else sGetFile = "" End If End Function Function pvToByteArray(sText As String) As Byte() pvToByteArray = StrConv(sText, vbFromUnicode) End Function 

我们发现问题是什么。 这确实是关于多部分请求的格式。 你需要非常小心的CrLf字符…

 Public Sub CreateForumPost() '... 'create forum post sBody = vbCrLf & "--" & sBoundary & vbCrLf & vbCrLf '... sBody = sBody & sGetFile("c:\temp\dummy.txt") & vbCrLf sBody = sBody & "--" & sBoundary & "--" '... End Sub 

现在起作用了。 尽pipe如此,非常感谢您的支持!