0

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

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

4

2 回答 2

1

根据 libcurl.net 文档: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

于 2016-05-22T17:32:06.983 回答
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) URL当然是第一件事

在我的情况下,我使用的是https://domain.com/confluence/rest/api/content/因为 Confluence 文档假设您将使用根域名(我也是如此)

然而,我不知道的是,给我测试的 URL 已经将我引导到“confluence”文件夹。

所以我的 URI 需要改为https://domain.com/rest/api/content/

(2) 需要将您的HTTPHEADER放入 Slist 的指示是从服务器返回:415 Unsupported Media Type

(3) 确保不要使用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 进程造成的。

“cleanup()”方法在 DLL 中不可用,尽管在示例中已被使用。

因此,请在程序结束时使用EASY.Dispose() ,此错误将停止。(我保留“GlobalCleanup()”方法只是为了更好地衡量)

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

但现在看来它没有,您可以简单地使用 .NET 中的“HttpWebRequest”类来获得相同的结果。

但是,陪审团仍然出局,因为我被分配到的“轻量级”测试服务器崩溃了,我正在等待他们修复它,以便我可以验证这一点。

不管怎样,我希望这一切对某人有帮助

于 2016-05-22T18:26:25.217 回答