libcurl使用笔记

curl:

GET: curl -vG -d “test=string”  http://127.0.0.1

不加-G是POST

直接curl 127.0.0.1是GET

直接curl -v  http://127.0.0.1/xxx?test=string&test2=string2 是GET,但是无法发送&test2=string2

POST: curl -vX POST -d “test=string”  http://127.0.0.1

 

libcurl:

libcurl returns CURLE_OK when the transfer went fine. Getting a 404 from a HTTP server is considered a fine transfer.

The response code is only going to be set if curl_easy_perform() returns CURLE_OK so you should check that first to make sure curl actually performed the request successfully.

POST请求到没有server的地址拿不到http code。只有GET能拿到。

用GET调curl_easy_setopt(curl_handle, CURLOPT_URL, url);的时候,url中必须是完整的请求http://127.0.0.1/xxx?test=string&test2=string2

用POST时url里只需要是http://127.0.0.1/xxx? ,然后再调curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, request);

 

OPT:

libcurl默认是用GET

/*
 * Initialize the UserDefined fields within a SessionHandle.
 * This may be safely called on a new or existing SessionHandle.
 */
CURLcode Curl_init_userdefined(struct UserDefined *set)
{
    set->httpreq = HTTPREQ_GET; /* Default HTTP request */
}

curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);相当于-v

    case CURLOPT_VERBOSE:
    /*
     * Verbose means infof() calls that give a lot of information about
     * the connection and transfer procedures as well as internal choices.
     */
    data->set.verbose = (0 != va_arg(param, long))?TRUE:FALSE;
    break;

 

curl_easy_setopt(curl_handle, CURLOPT_POST, 1);1为用POST,0为用GET

    case CURLOPT_POST:
    /* Does this option serve a purpose anymore? Yes it does, when
       CURLOPT_POSTFIELDS isn't used and the POST data is read off the
       callback! */
    if(va_arg(param, long)) {
      data->set.httpreq = HTTPREQ_POST;
      data->set.opt_no_body = FALSE; /* this is implied */
    }
    else
      data->set.httpreq = HTTPREQ_GET;
    break;

 

curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, 1);也是用POST

    case CURLOPT_COPYPOSTFIELDS:
    /*
     * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
     * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
     *  CURLOPT_COPYPOSTFIELDS and not altered later.
     */
    data->set.httpreq = HTTPREQ_POST;
    break;

 

curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);1为用GET,0啥也不干

    case CURLOPT_HTTPGET:
    /*
     * Set to force us do HTTP GET
     */
    if(va_arg(param, long)) {
      data->set.httpreq = HTTPREQ_GET;
      data->set.upload = FALSE; /* switch off upload */
      data->set.opt_no_body = FALSE; /* this is implied */
    }
    break;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值