java libcurl post参数,CURL中的Post方法

Suppose we have a HTTP request like following:

POST /safebrowsing/downloads?client=Firefox&appver=3.0.8&pver=2.2&wrkey=AKEgNiux-3bBzAgJeFWgqbneh_GLc2OrmgXnpxPrdH1-hFpbAM8k1ovPA8GB_UMRueBHnL3QJ7gsdQOWVm6QJr_VZNgAm8jmLQ== HTTP/1.1

Host: safebrowsing.clients.google.com

User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko/2009033100 Ubuntu/9.04 (jaunty) Firefox/3.0.8

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Content-Length: 120

Content-Type: text/plain

Cookie: PREF=ID=551a1b8848e36099:U=e6fa7464d7c48884:FF=0:TM=1327553284:LM=1345022478:S=Qd0IssyrqLi17a4s; NID=62=R9Y5bkQ5iLF8zlyhma1gnRBfxPDoWuG2FibC2wc5u0eAIQgAuo4WCXMeLhdPZ7FXJEpN2Sw1a6da6QUNP7OC5OqTYK0Y39vd6c2fUh4BhY2B5CGsKtHuQ5RCpSnefSkb

goog-malware-shavar;a:83372-91327:s:59904-95254:mac

goog-phish-shavar;a:227421-233955,235041-235401:s:107142-110470:mac

I already built up the part of code to handle the headers. However, the message-body part I'm not sure how to deal with. I have read CURL sample code, they provide solution for HTTP form POST which is not the way to handle my data. Does anyone know what parameter I should use to handle message-body using curl_easy_setopt() function?

解决方案

FYI: cURL provides a very convenient option that lists the ad-hoc options for a given HTTP request:

--libcurl Dump libcurl equivalent code of this command line

When in doubt you should perform your request via cURL (and corresponding options, e.g. to perform a POST, etc) while using this option since it outputs the list of curl_easy_setopt options:

curl --libcurl out.c http://stackoverflow.com/ > /dev/null

Then open the out.c file within your editor:

...

curl_easy_setopt(hnd, CURLOPT_URL, "http://stackoverflow.com/");

curl_easy_setopt(hnd, CURLOPT_PROXY, NULL);

curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0);

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用libcurl库进行POST方法上传文件的步骤如下: 1. 初始化curl库:`curl_global_init(CURL_GLOBAL_ALL);` 2. 创建curl句柄:`CURL* curl = curl_easy_init();` 3. 设置请求URL:`curl_easy_setopt(curl, CURLOPT_URL, url);` 4. 设置请求方式为POST:`curl_easy_setopt(curl, CURLOPT_POST, 1L);` 5. 设置上传的文件:`curl_easy_setopt(curl, CURLOPT_READDATA, file);` 其,`file`为你要上传的文件的文件指针。 6. 设置上传的文件大小:`curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, file_size);` 其,`file_size`为你要上传的文件的大小。 7. 设置上传的文件名:`curl_easy_setopt(curl, CURLOPT_POSTFIELDS, file_name);` 其,`file_name`为你要上传的文件的文件名。 8. 执行请求:`curl_easy_perform(curl);` 9. 清理curl句柄:`curl_easy_cleanup(curl);` 完整的示例代码如下: ``` #include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; FILE *file; const char *url = "http://example.com/upload.php"; const char *file_name = "example.txt"; long file_size; file = fopen(file_name, "rb"); if (!file) { fprintf(stderr, "Failed to open file '%s'\n", file_name); return 1; } fseek(file, 0L, SEEK_END); file_size = ftell(file); rewind(file); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_POST, 1L); curl_easy_setopt(curl, CURLOPT_READDATA, file); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, file_size); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, file_name); res = curl_easy_perform(curl); if (res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } fclose(file); curl_global_cleanup(); return 0; } ``` 注意,这只是一个简单的示例,实际应用还需要添加错误处理和其他必要的选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值