3

我正在尝试使用 libcurl 构建一个 HTTP 表单,但我无法让它正常工作。每次我调用curl_formadd它都会返回CURL_FORMADD_OPTION_TWICE。有关此错误的唯一信息表明 libcurl 认为我正在尝试添加两个具有相同名称的表单元素,即使它是第一次调用curl_formadd并且我只添加一个元素!

  Declare Function curl_global_init Lib "libcurl" (flags As Integer) As Integer
  Declare Function curl_formadd Lib "libcurl" (FirstItem As Ptr, LastItem As Ptr, Option1 As Integer, Value1 As Ptr, Option2 As Integer, Value2 As Ptr, EndMarker As Integer) As Integer

  Const CURLFORM_COPYCONTENTS = 2
  Const CURLFORM_COPYNAME = 1
  Const CURLFORM_END = 17

  Dim formname, formvalue As MemoryBlock
  formname = "NAME"
  formvalue = "CONTENTS"

  If curl_global_init(3) = 0 Then
    Dim first, last As Ptr
    Dim err As Integer
    err = curl_formadd(first, last, CURLFORM_COPYNAME, formname, CURLFORM_COPYCONTENTS, formvalue, CURLFORM_END)
    Break
    ' err is 2 (CURL_FORMADD_OPTION_TWICE) 
  End If

这个错误想告诉我什么?

4

1 回答 1

3

阅读 curl_formadd() 的“手册”页面,它说:

在第一次调用此函数时,指针 firstitem 和 lastitem 都应该指向 NULL。

但是,您为这些传递 NULL。

您似乎也错误地传递了字符串。尝试将 Value1 和 Value2 参数定义为“作为 CString”,然后传递普通字符串,而不是 Memoryblocks。

最后,您给了 CURLFORM_COPYCONTENTS 错误的代码。它不是 2 而是 4。请参阅 curl.h 中的 CURLformoption 枚举:“CFINIT(NOTHING)”的值为 0,超过该值的每个项目都会变高,因此 CFINIT(COPYCONTENTS) 的值为 4。

这是对我有用的代码:

Declare Function curl_global_init Lib "libcurl" (flags As Integer) As Integer
Declare Function curl_formadd Lib "libcurl" (ByRef FirstItem As Ptr, ByRef LastItem As Ptr, Option1 As Integer, Value1 As CString, Option2 As Integer, Value2 As CString, EndMarker As Integer) As Integer

Const CURLFORM_COPYCONTENTS = 4
Const CURLFORM_COPYNAME = 1
Const CURLFORM_END = 17
const CURL_GLOBAL_ALL = 3

Dim formname, formvalue As String
formname = "NAME"
formvalue = "CONTENTS"

If curl_global_init(CURL_GLOBAL_ALL) = 0 Then
  Dim first, last As Ptr
  Dim err As Integer
  err = curl_formadd(first, last, CURLFORM_COPYNAME, formname, CURLFORM_COPYCONTENTS, formvalue, CURLFORM_END)
  Break
  ' err is 0
End If
于 2014-05-02T07:55:28.423 回答