可以使用libcurl.net将新页面发布到汇合点吗?

时间:2016-05-21 19:51:49

标签: confluence-rest-api libcurl.net

我刚刚开始使用REST API来创建页面。

我正在尝试配置一个基本示例,我想使用libcurl.net来实现它。

有没有人看到为什么不起作用的原因?

更新

以下是我迄今为止改编自curllib.net“bookpost”示例

的内容
 Option Explicit On
Imports System.IO
Imports System.Net
Imports SeasideResearch.LibCurlNet

Public Class CurlOperations

Public Shared Sub CurlPost()

    Try

        Dim credUserName As String = "username"
        Dim credPassword As String = "password"

        Dim response As String = Nothing
        Dim outputStdErr As Stream = Nothing

        Curl.GlobalInit(CURLinitFlag.CURL_GLOBAL_ALL)

        Dim easy As Easy
        easy = New Easy

        ' Set up write delegate
        Dim wf As Easy.WriteFunction
        wf = New Easy.WriteFunction(AddressOf OnWriteData)
        easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf)

        'Authentication
        easy.SetOpt(CURLoption.CURLOPT_HTTPAUTH, CURLhttpAuth.CURLAUTH_BASIC)
        easy.SetOpt(CURLoption.CURLOPT_USERPWD, credUserName & ":" & credPassword)

        'disable ssl peer verification
        easy.SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, False)

        'Header
        easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, "Content-Type: application/json; charset=utf-8")

        ' Simple post - with a string
        easy.SetOpt(CURLoption.CURLOPT_POSTFIELDS, WikiTools.CommREST.WebToCF.PostCurl())

        ' and the rest of the cURL options
        easy.SetOpt(CURLoption.CURLOPT_USERAGENT, ".NET Framework Client")
        easy.SetOpt(CURLoption.CURLOPT_URL, "https://domain.com/confluence/rest/api/content/")
        easy.SetOpt(CURLoption.CURLOPT_POST, True)

        response = easy.Perform().ToString
        LoggingAndActivites.WriteLog("Response: " & response, GetFunctionName.GetCallerName, True, True)

    Catch ex As Exception

        LoggingAndActivites.WriteLog("Exception: " & ex.ToString, GetFunctionName.GetCallerName, True, True)

    End Try

    Curl.GlobalCleanup()

End Sub

' Called by libcURL.NET when it has data for your program
Public Shared Function OnWriteData(ByVal buf() As Byte, ByVal size As Int32, ByVal nmemb As Int32, ByVal extraData As Object) As Int32

    LoggingAndActivites.WriteLog(System.Text.Encoding.UTF8.GetString(buf), GetFunctionName.GetCallerName, True, True)

    Return size * nmemb

End Function

 End Class

我正在连接,因为如果删除用户名和密码,我会通过“onWriteData”函数得到响应,如下所示:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>401 Unauthorized</title>
</head>
<body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<hr>
<address>Apache Server at domain.com Port 7080</address>
</body>
</html>

现在的问题是,如果我正确登录,除了“easy.Perform()”函数中的“CURLE_OK”之外,我没有得到任何响应。

这很好,因为我知道它在某种程度上有效。

2 个答案:

答案 0 :(得分:1)

根据libcurl.net docs:http://www.libcurl.org/

  

libcurl还支持HTTPS证书,HTTP POST,HTTP PUT,FTP上传,基于HTTP表单的上传,代理,cookie和用户+密码验证。

所以我猜你应该能够用它进行REST API调用。我使用curl(linux版本)来创建和更新页面,使用类似这样的东西:

curl --globoff --insecure --silent -u username:password -X PUT -H 'Content-Type: application/json' --data @body.json confluenceRestAPIURL/pageId

其中body.json是一个包含更新页面数据的文件。

我在这里写了一篇博客:https://javamemento.blogspot.no/2016/05/jira-confluence-3.html

您可以在此处获取代码:https://github.com/somaiah/jira-confluence-graphs

答案 1 :(得分:0)

所以它确实有用

以下是我添加/更改以使上述代码正常工作的内容

'I added an Slist to store the header items (I only had one)            
Dim slistHeaders As New Slist
slistHeaders.Append("Content-Type: application/json")

'Then I added the slist to the HTTPHEADER
easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, slistHeaders)

需要注意的事项:

(1)网址当然是排名第一

在我的情况下,我使用的是https://domain.com/confluence/rest/api/content/,因为Confluence文档假定您将使用根域名(就像我一样)

然而,我不知道的是,我给予测试的网址已经指引我进入&#34;汇合&#34;文件夹中。

所以我的URI必须是https://domain.com/rest/api/content/而不是

(2)需要将 HTTPHEADER 放入Slist的指示符是从服务器返回:415不支持的媒体类型

(3)确保 NOT 使用 CURLOPT_HEADER 属性。如果您在回复中看到此标题,则需要确保未使用此标题:

HTTP/1.1 500 Internal Server Error
Date: Sun, 22 May 2016 17:50:32 GMT
Server: Apache
Content-Location: 500.en-GB.html
Vary: negotiate,accept-language
TCN: choice
Accept-Ranges: bytes
Content-Length: 7575
Connection: close
Content-Type: text/html; charset=UTF-8
Content-Language: en-gb

请参阅我的帖子,了解原因:CURLOPT_HEADER

(4)最后,当您收到此错误时构建应用程序:

An unhandled exception of type 'System.AccessViolationException' occurred in libcurl.NET.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

这是由libcurl.net进程未清理造成的。

&#34;清理()&#34;虽然在示例中有所提及,但DLL中的方法并不可用。

因此,在程序结束时使用 EASY.Dispose(),此错误将停止。 (我保留了#34; GlobalCleanup()&#34;方法以备好的方法)

(5)具有讽刺意味的是,我采用了CURL的方式,因为我认为Confluence接口可能需要它。

但现在看来它并没有,你可以简单地使用&#34; HttpWebRequest&#34; .NET中的类可以获得相同的结果。

然而,陪审团还是因为&#34;轻量级&#34;测试服务器我被分配到崩溃,我等着他们修复它,所以我可以验证这一点。

无论哪种方式,我希望这一切都有助于某人

中号