我试图使用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
这个错误想告诉我什么?
阅读curl_formadd()的"man"页面,它显示:
在对该函数的第一次调用中,指针firstitem和lastitem都应该指向NULL。
但是,您可以为这些传递NULL。
您似乎也错误地传递了字符串。尝试将Value1和Value2参数定义为"CString",然后传递普通的Strings,而不是Memoryblock。
最后,您给了CURLFORM_COPYCONTENTS错误的代码。不是2而是4。请参阅curl.h中的CURLformoption枚举:"CFINIT(NOTHING)"的值为0,超过该值的每一项都会高出一个,因此CFNIT(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