首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >libcurl.net可以用来发布一个新的页面到汇合处吗?

libcurl.net可以用来发布一个新的页面到汇合处吗?
EN

Stack Overflow用户
提问于 2016-05-21 19:51:48
回答 2查看 216关注 0票数 0

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

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

有人知道为什么这样不行吗?

更新:

到目前为止,我已经从curllib.net的"bookpost“示例中改编了以下内容

代码语言:javascript
复制
 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“函数得到如下响应:

代码语言:javascript
复制
<!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>

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-22 18:26:25

所以它起作用了

下面是我为使上面的代码工作而添加/更改的内容

代码语言:javascript
复制
'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当然是的头等大事

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

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

所以我的URI应该是https://domain.com/rest/api/content/,而不是

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

(3)确保使用CURLOPT_HEADER属性的是而不是。如果您在响应中看到这个标题,您需要确保它没有被使用:

代码语言:javascript
复制
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

有关为什么:HEADER的解释,请参阅我在这里的文章

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

代码语言:javascript
复制
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进程未被清理所致。

尽管示例中列出了“清除()”方法,但它在DLL中是不可用的。

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

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

但是现在看来并非如此,您可以简单地使用.NET中的“.NET”类来获得相同的结果。

然而,由于我被分配到的“轻量级”测试服务器崩溃了,我正在等待他们修复它,所以我可以验证这一点,所以仍然没有定论。

不管怎样,我希望这一切都能帮到一个人

票数 0
EN

Stack Overflow用户

发布于 2016-05-22 17:32:06

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

libcurl还支持HTTPS证书、HTTPS、HTTPS、FTP上载、基于HTTP表单的上载、代理、cookie和user+password身份验证。

因此,我想您应该能够使用它进行REST调用。我使用curl ( linux版本)创建和更新页面,使用如下所示:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37367352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档