利用curl来做http访问(支持session,cookie)
2006-03-24 10:37
501 查看
/* curl_httpget.h */
#ifndef CURL_HTTPGET_H
#define CURL_HTTPGET_H
struct MemoryStruct {
char *memory;
size_t size;
};
int curl_httpget(const char* URLbuf,struct MemoryStruct *chunk,const char* cookiefile);
#endif
/* curl_httpget.cpp */
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "curl_httpget.h"
#include <alloc.h>
void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
int curl_httpget(const char* URLbuf,struct MemoryStruct *chunk,const char* cookiefile)
{
CURL *curl_handle;
int errcode=0;
chunk->memory=NULL; /* we expect realloc(NULL, size) to work */
chunk->size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, URLbuf);
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE,cookiefile);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR,cookiefile);
/* get it! */
errcode=curl_easy_perform(curl_handle);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return errcode;
}
用法:
errcode= curl_httpget(URLbuf,&chunk,cookiefile);
if(!errcode){
int min=sizeof(content)>chunk.size?chunk.size:sizeof(content);
strncpy(content,chunk.memory,min);
}else{
strcpy(content,"");
}
if(chunk.memory) free(chunk.memory);
#ifndef CURL_HTTPGET_H
#define CURL_HTTPGET_H
struct MemoryStruct {
char *memory;
size_t size;
};
int curl_httpget(const char* URLbuf,struct MemoryStruct *chunk,const char* cookiefile);
#endif
/* curl_httpget.cpp */
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "curl_httpget.h"
#include <alloc.h>
void *myrealloc(void *ptr, size_t size)
{
/* There might be a realloc() out there that doesn't like reallocing
NULL pointers, so we take care of it here */
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
}
size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
int curl_httpget(const char* URLbuf,struct MemoryStruct *chunk,const char* cookiefile)
{
CURL *curl_handle;
int errcode=0;
chunk->memory=NULL; /* we expect realloc(NULL, size) to work */
chunk->size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, URLbuf);
curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE,cookiefile);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR,cookiefile);
/* get it! */
errcode=curl_easy_perform(curl_handle);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return errcode;
}
用法:
errcode= curl_httpget(URLbuf,&chunk,cookiefile);
if(!errcode){
int min=sizeof(content)>chunk.size?chunk.size:sizeof(content);
strncpy(content,chunk.memory,min);
}else{
strcpy(content,"");
}
if(chunk.memory) free(chunk.memory);
相关文章推荐
- Session是通过利用设置在Cookie中的id来区分访问的用户
- 如何利用session,cookie来解决http无状态本质
- 利用AFNetworking 的AFHTTPSessionManager代码实现代理的方式访问任意指定IP地址,解决域名DNS上的问题
- 安卓课程表(解决利用Httpclient登录获得cookie继续访问但网页仍提示无权限请登录的问题)
- 解决不支持cookie的手机访问wap(session)
- 利用HttpHandler实现验证码 及 当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.
- HttpQuery httpClient with cookie sessionID
- 通过设置P3P头来实现跨域访问COOKIE_老王的技术手册 ( 我的新博客:http://huoding.com )_百度空间
- centos7搭建svn服务器并支持http方式访问
- cookie 和session 的区别详解 (出处:http://shiyangxt.cnblogs.com )
- 7.让网站支持http和https的访问方式
- URL 授权访问另外一种方法,利用 Java 1.1 访问密码保护的 URL(9588http短信通道接入)
- curl模拟访问已经存在的cookie
- linux 命令访问url: curl http://www.baidu.com/index.html
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例
- 采用Apache服务器,利用http协议格式来访问另一台电脑的文件夹
- HTTP中Cookie,Session的理解
- 使用 ngx_http_upstream_session_sticky_module 实现基于cookie的负载均衡
- HTTP协议基础(渗透基础:状态码,cookie,session)
- HTTP协议中session与cookie的联系与区别