2014-04-17 71 views
3

我試圖用libcurl構造一個HTTP表單,但是我無法讓它正常工作。每次我撥打curl_formadd時,都會返回CURL_FORMADD_OPTION_TWICE。關於這個錯誤的唯一信息表明libcurl認爲我試圖添加兩個具有相同名稱的表單元素,即使它第一次調用curl_formadd,而我只添加了一個元素!curl_formadd在第一次調用時返回`CURL_FORMADD_OPTION_TWICE`

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 

什麼是這個錯誤試圖告訴我?

回答

3

閱讀curl_formadd()的「人」頁面,它說:

指針和與firstItem應lastitem無論是在第一次調用這個函數是指向NULL。

但是,您爲這些傳遞NULL。

您似乎也錯誤地傳遞了字符串。嘗試將Value1和Value2參數定義爲「CString」,然後傳遞正常的字符串,而不是內存塊。

最後,你給了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 
+0

謝謝,但我仍然得到'CURL_FORMADD_OPTION_TWICE'。 –

+0

錯誤代碼2並不意味着CURL_FORMADD_OPTION_TWICE,而是列出瞭如下所述的錯誤代碼:「非零意味着發生了錯誤,對應於在中定義的CURL_FORMADD_ *常量」 –

+0

「CURL_FORMADD_OPTION_TWICE」是具有「CURL_FORMADD_ *值爲2.如果這不正確,'2'表示什麼? –

相關問題