libcurl.net可以用来post一个新的页面汇合吗?

Can libcurl.net be used to post a new page to confluence?

我刚刚开始使用 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" 之外,我不会得到任何响应。

很好,因为我知道它在某种程度上起作用。

根据 libcurl.net 文档:http://www.libcurl.org/

libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user+password authentication.

所以我想您应该可以用它进行 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

所以它确实有效

这是我added/changed使上面的代码工作的方法

'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) URL当然是头等大事

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

然而,我不知道的是,让我测试的 URL 已经将我引导到 "confluence" 文件夹中。

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

(2) 您的 HTTPHEADER 需要放入 Slist 的一个指标是来自服务器的 return:415 Unsupported Media Type

(3) 确保 NOT 使用 CURLOPT_HEADER 属性。如果您在回复中看到此 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

参考我的post这里解释原因: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 进程没有被清理造成的。

"cleanup()" 方法在 DLL 中不可用,尽管示例中有它。

因此,请在程序结束时使用 EASY.Dispose(),此错误将停止。 (我保留了 "GlobalCleanup()" 方法只是为了好的措施)

(5) 具有讽刺意味的是,我选择了使用 CURL 的方式,因为我认为 Confluence 界面可能需要它。

但现在看来并非如此,您只需在 .NET 中使用 "HttpWebRequest" Class 即可获得相同的结果。

然而,陪审团仍然没有定论,因为我被分配到的 "lightweight" 测试服务器崩溃了,我正在等待他们修复它,以便我可以验证这一点。

无论如何,我希望所有这些对某人有所帮助