使用libcurl在android上实现webdav客户端

时间:2014-10-01 08:33:05

标签: android curl libcurl webdav

我想在android中实现libcurl函数。原始卷曲命令如下:

curl -k  --user "username:password" --header "Depth: 1" --data "data" -X PROPFIND https://device_ipaddr/device_name > /tmp/AiStorageList

以下是我在android jni中的代码:

CURL *curl;
CURLcode res;
std::string content;

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();

if(curl) {
    struct curl_slist *headers = NULL;
    char *data="data";
    headers = curl_slist_append(headers, "Depth: 1");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER,headers);
    curl_easy_setopt(curl, CURLOPT_URL, "https://device_ipaddr/device_name");
    curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, "username:password");
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PROPFIND");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Post_Response);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &content);

    res = curl_easy_perform(curl);

    LOGD("res: %d\n", res);
    LOGD("content = %s \n", content.c_str());
    curl_easy_cleanup(curl);
}

curl_global_cleanup();
return 0;

这是Post_Response函数:

static long Post_Response(void *data, int size, int nmemb, std::string &content)
{
    long sizes = size * nmemb;
    std::string temp((char*)data,sizes);
    content += temp;

    return sizes;
}

我总是得到res" 1",这意味着在激活curl_easy_perform时发生了错误,内容也是空的。
非常感谢任何帮助 提前谢谢。

0 个答案:

没有答案