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;