windows下面使用libcurl

windows下面使用libcurl

  946人阅读  评论(0)  收藏  举报

1. 下载安装

下载 libcurl -- http://curl.haxx.se/dlwiz/

tortoise svn has libsasl library

 

2. 设置windows环境变量

LIBCURL_ROOT -- libcurl 存放路径,比如 E:/opensdk/libcurl-7.19.3

 

3. 设置VS工程属性,添加$(LIBCURL_ROOT)/include, $(LIBCURL_ROOT)/lib, 动态链接添加curllib.lib,静态链接添加curllib_static.lib

静态链接需添加如下东西:

#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "wldap32.lib" )

CURL_STATICLIB


If you’re using libcurl as a win32 DLL, you MUST use the CURLOPT_WRITEFUNCTION if you set CURLOPT_
WRITEDATA - or you will experience crashes.

 

4. 使用libcurl操作HTTP协议必须考虑的问题:

4.1 操作是同步还是异步的?

easy 接口是同步。

4.2 是否线程安全? 

绝对不应该在线程之间共享同一个libcurl handle,不管是easy handle还是multi handle, libcurl是线程安全的, 其它依赖的库是否线程安全?

4.3 是否可在高并发情况下使用?

4.4 使用何种license(MIT)

4.5 如何设置http 请求的方法(POST, GET or other), 头部,协议版本号(HTTP/1.0 or HTTP/1.1)

4.6 如何获得返回的status code, 头部,协议版本号(HTTP/1.0 or HTTP/1.1)

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "SETUP");

 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

4.7 

CURLOPT_HEADERFUNCTION,CURLOPT_HEADERDATA
回调函数原型为 size_t function( void *ptr, size_t size,size_t nmemb, void *stream); libcurl一旦接收到http 头部数据后将调用该函数。CURLOPT_WRITEDATA 传递指针给libcurl,该指针表明CURLOPT_HEADERFUNCTION 函数的stream指针的来源


 


// 注册回调函数
curl_easy_setopt(easy_handle, CURLOPT_READFUNCTION, read_function); 
// 设置自定义指针
curl_easy_setopt(easy_handle, CURLOPT_READDATA, &filedata); 
curl_easy_setopt(easy_handle, CURLOPT_UPLOAD, 1L); 

// 设置上传文件大小
curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE_LARGE, file_size);


5. libcurl 几个重要函数

curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2000);

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

curl_slist* headers = NULL;

headers = curl_slist_append(headers, "Content-Type: text/xml");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

curl_httppost* post = NULL;

curl_httppost* last = NULL;
curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "zheng", CURLFORM_END);

curl_formadd(&post, &last, CURLFORM_COPYNAME, "project", CURLFORM_COPYCONTENTS, "curl", CURLFORM_END);


curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);


static size_t process_data(void* ptr, size_t size, size_t nmemb, void* param) {
  return size * nmemb;
}


curl_easy_setopt(easy_handle, CURLOPT_USERPWD, "user_name:password");


通过将CURLOPT_HTTPGET设为1可以使easy handle回到最原始的状态

curl_easy_setopt(easy_handle, CURLOPT_HTTPGET, 1L);

每个easy handle都会保存最近使用的几个连接,以备重用。默认是5个。可以通过CURLOPT_MAXCONNECTS属性来设置保存连接的数量。  
如果你不想重用连接,将CURLOPT_FRESH_CONNECT属性设置为1。这样每次提交请求时,libcurl都会先关闭以前创建的连接,然后重新创建一个新的连接。
也可以将CURLOPT_FORBID_REUSE设置为1,这样每次执行完请求,连接就会马上关闭
 
 
http://blog.csdn.net/mjpassion/article/details/6290912
JNI是Java Native Interface的缩写,该技术允许Java应用程序调用本地编写的C/C++代码。因此,您可以使用JNI和libcurl库在Java应用程序中进行POST请求。 下面是一个使用JNI和libcurl库进行完整的POST提交的示例代码: 1. Java代码 ```java public class CurlWrapper { static { System.loadLibrary("curlwrapper"); } public native String post(String url, String postData); } ``` 2. C/C++代码 ```c++ #include <jni.h> #include <curl/curl.h> #include <string.h> JNIEXPORT jstring JNICALL Java_CurlWrapper_post(JNIEnv *env, jobject obj, jstring url, jstring postData) { CURL *curl; CURLcode res; const char *urlStr = env->GetStringUTFChars(url, 0); const char *postDataStr = env->GetStringUTFChars(postData, 0); char *response = NULL; long responseCode; size_t responseLen = 0; struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded"); curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, urlStr); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postDataStr); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); res = curl_easy_perform(curl); if (res == CURLE_OK) { curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode); curl_easy_cleanup(curl); curl_slist_free_all(headers); responseLen = strlen(response); jstring result = env->NewStringUTF(response); free(response); env->ReleaseStringUTFChars(url, urlStr); env->ReleaseStringUTFChars(postData, postDataStr); return result; } else { curl_easy_cleanup(curl); curl_slist_free_all(headers); free(response); env->ReleaseStringUTFChars(url, urlStr); env->ReleaseStringUTFChars(postData, postDataStr); return NULL; } } } size_t writeFunc(char *ptr, size_t size, size_t nmemb, void *userdata) { size_t len = size * nmemb; char **response = (char **) userdata; *response = (char *) realloc(*response, len + 1); if (*response) { memcpy(*response, ptr, len); (*response)[len] = '\0'; } return len; } ``` 3. 编译 在Linux下,您可以使用以下命令编译C/C++代码: ``` gcc -shared -fPIC -o libcurlwrapper.so -I$JAVA_HOME/include -I$JAVA_HOME/include/linux curlwrapper.cpp -lcurl ``` 在Windows下,您可以使用以下命令编译C/C++代码: ``` gcc -shared -o curlwrapper.dll -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" curlwrapper.cpp -lcurl ``` 4. 调用 在Java代码中,您可以使用以下代码调用CurlWrapper.post方法: ```java CurlWrapper curlWrapper = new CurlWrapper(); String response = curlWrapper.post("https://example.com/api/", "param1=value1&param2=value2"); ``` 其中,第一个参数是目标URL,第二个参数是POST请求的数据。如果请求成功,该方法将返回响应文本,否则将返回null。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值