libevent post下载文件
来源:互联网 发布:产品样本设计软件 编辑:程序博客网 时间:2024/06/04 08:48
#if 0
//post方式从远程服务器下载一个文件
static struct event_base *base;
static void
http_request_file_done(struct evhttp_request *req, void *ctx)
{
char buffer[4097];
int nread;
printf("http_request_file_done");
if (req == NULL) {
/* If req is NULL, it means an error occurred, but
* sadly we are mostly left guessing what the error
* might have been. We'll do our best... */
struct bufferevent *bev = (struct bufferevent *) ctx;
unsigned long oslerr;
int printed_err = 0;
int errcode = EVUTIL_SOCKET_ERROR();
printf("some request failed - no idea which one though!\n");
/* Print out the OpenSSL error queue that libevent
* squirreled away for us, if any. */
if (!printed_err)
printf("socket error = %s (%d)\n",
evutil_socket_error_to_string(errcode),
errcode);
return;
}
printf("Response line: %d\n",
evhttp_request_get_response_code(req));
//unsigned char _down_data[1024];
while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
buffer, sizeof(buffer)-1))
> 0) {
/* These are just arbitrary chunks of 256 bytes.
* They are not lines, so we can't treat them as such. */
printf( "nread = %d\n", nread );
printf( "nreadq = %s\n", buffer );
printf( "buffer[0] = %x\n", buffer[0] );
printf( "buffer[1] = %x\n", buffer[1] );
printf( "buffer[2] = %x\n", buffer[2] );
printf( "buffer[3] = %x\n", buffer[3] );
buffer[nread] = 0;
//strcpy(&_gjson[_len], buffer);
//memcpy(&_download_file_buffer[_download_file_len], buffer, nread);
//_download_file_len += nread;
}
//_http_flag = 1;
event_base_loopbreak(base);
}
int http_get_file(char *_url, unsigned char *download_file_buffer)
{
int r;
unsigned char *_download_file_buffer;
_download_file_buffer = download_file_buffer;
struct evhttp_uri *http_uri = NULL;
char url[256], data_file[256];
const char *crt = NULL;
const char *scheme, *host, *path, *query;
char uri[256];
int port;
int retries = 0;
int timeout = -1;
struct bufferevent *bev;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req;
struct evkeyvalq *output_headers;
struct evbuffer *output_buffer;
int i;
int ret = 0;
enum { HTTP, HTTPS } type = HTTP;
char _filename[] = "post_file.c";
sprintf(url, "%s", _url);
sprintf(data_file, "%s%s", "C:\\Users\\zengyongheng\\Desktop\\", _filename);
http_uri = evhttp_uri_parse(url);
if (http_uri == NULL) {
printf("malformed url");
goto error;
}
scheme = evhttp_uri_get_scheme(http_uri);
if (scheme == NULL || (strcasecmp(scheme, "http") != 0 &&
strcasecmp(scheme, "http") != 0)) {
printf("url must be http or https");
goto error;
}
host = evhttp_uri_get_host(http_uri);
if (host == NULL) {
printf("url must have a host");
goto error;
}
port = evhttp_uri_get_port(http_uri);
if (port == -1) {
port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
}
path = evhttp_uri_get_path(http_uri);
if (strlen(path) == 0) {
path = "/";
}
query = evhttp_uri_get_query(http_uri);
if (query == NULL) {
sprintf(uri, "%s", path);
}
else {
sprintf(uri, "%s?%s", path, query);
}
uri[sizeof(uri)-1] = '\0';
// Create event base
base = event_base_new();
if (!base) {
perror("event_base_new()");
goto error;
}
// For simplicity, we let DNS resolution block. Everything else should be
// asynchronous though.
//evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port);
evcon = evhttp_connection_base_new(base, NULL, host, port);
if (evcon == NULL) {
fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
goto error;
}
if (retries > 0) {
evhttp_connection_set_retries(evcon, retries);
}
if (timeout >= 0) {
evhttp_connection_set_timeout(evcon, timeout);
}
//bev = evhttp_connection_get_bufferevent(evcon);
//if (bev == NULL) {
// printf("evhttp_connection_get_bufferevent() failed\n");
// goto error;
// }
// Fire off the request
req = evhttp_request_new(http_request_file_done, bev);
if (req == NULL) {
fprintf(stderr, "evhttp_request_new() failed\n");
goto error;
}
char tmp_buf[256] = { 0 };
output_headers = evhttp_request_get_output_headers(req);
evhttp_add_header(output_headers, "Host", host);
evhttp_add_header(output_headers, "Connection", "keep-alive");
r = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, uri);
if (r != 0) {
fprintf(stderr, "evhttp_make_request() failed\n");
goto error;
}
event_base_dispatch(base);
goto cleanup;
error:
ret = 1;
cleanup:
if (evcon)
evhttp_connection_free(evcon);
if (http_uri)
evhttp_uri_free(http_uri);
event_base_free(base);
return ret;
}
int main( int c, char **v )
{
int idx;
int reverse = 0, servertest = 0, use_getaddrinfo = 0;
struct event_base *event_base = NULL;
struct evdns_base *evdns_base = NULL;
if( c < 2 ){
fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
return 1;
}
idx = 1;
while (idx < c && v[idx][0] == '-') {
if (!strcmp(v[idx], "-x"))
reverse = 1;
else if (!strcmp(v[idx], "-v"))
verbose = 1;
else if (!strcmp(v[idx], "-g"))
use_getaddrinfo = 1;
else if (!strcmp(v[idx], "-servertest"))
servertest = 1;
else
fprintf(stderr, "Unknown option %s\n", v[idx]);
++idx;
}
#ifdef WIN32
{
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
}
#endif
http_get_file("http://172.16.1.18:8080/test.bin", "");
//http_get_(REQ_TEST0);
//fflush(stdout);
printf("debug4\n" );
return 0;
}
#endif
//post方式从远程服务器下载一个文件
static struct event_base *base;
static void
http_request_file_done(struct evhttp_request *req, void *ctx)
{
char buffer[4097];
int nread;
printf("http_request_file_done");
if (req == NULL) {
/* If req is NULL, it means an error occurred, but
* sadly we are mostly left guessing what the error
* might have been. We'll do our best... */
struct bufferevent *bev = (struct bufferevent *) ctx;
unsigned long oslerr;
int printed_err = 0;
int errcode = EVUTIL_SOCKET_ERROR();
printf("some request failed - no idea which one though!\n");
/* Print out the OpenSSL error queue that libevent
* squirreled away for us, if any. */
if (!printed_err)
printf("socket error = %s (%d)\n",
evutil_socket_error_to_string(errcode),
errcode);
return;
}
printf("Response line: %d\n",
evhttp_request_get_response_code(req));
//unsigned char _down_data[1024];
while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
buffer, sizeof(buffer)-1))
> 0) {
/* These are just arbitrary chunks of 256 bytes.
* They are not lines, so we can't treat them as such. */
printf( "nread = %d\n", nread );
printf( "nreadq = %s\n", buffer );
printf( "buffer[0] = %x\n", buffer[0] );
printf( "buffer[1] = %x\n", buffer[1] );
printf( "buffer[2] = %x\n", buffer[2] );
printf( "buffer[3] = %x\n", buffer[3] );
buffer[nread] = 0;
//strcpy(&_gjson[_len], buffer);
//memcpy(&_download_file_buffer[_download_file_len], buffer, nread);
//_download_file_len += nread;
}
//_http_flag = 1;
event_base_loopbreak(base);
}
int http_get_file(char *_url, unsigned char *download_file_buffer)
{
int r;
unsigned char *_download_file_buffer;
_download_file_buffer = download_file_buffer;
struct evhttp_uri *http_uri = NULL;
char url[256], data_file[256];
const char *crt = NULL;
const char *scheme, *host, *path, *query;
char uri[256];
int port;
int retries = 0;
int timeout = -1;
struct bufferevent *bev;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req;
struct evkeyvalq *output_headers;
struct evbuffer *output_buffer;
int i;
int ret = 0;
enum { HTTP, HTTPS } type = HTTP;
char _filename[] = "post_file.c";
sprintf(url, "%s", _url);
sprintf(data_file, "%s%s", "C:\\Users\\zengyongheng\\Desktop\\", _filename);
http_uri = evhttp_uri_parse(url);
if (http_uri == NULL) {
printf("malformed url");
goto error;
}
scheme = evhttp_uri_get_scheme(http_uri);
if (scheme == NULL || (strcasecmp(scheme, "http") != 0 &&
strcasecmp(scheme, "http") != 0)) {
printf("url must be http or https");
goto error;
}
host = evhttp_uri_get_host(http_uri);
if (host == NULL) {
printf("url must have a host");
goto error;
}
port = evhttp_uri_get_port(http_uri);
if (port == -1) {
port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
}
path = evhttp_uri_get_path(http_uri);
if (strlen(path) == 0) {
path = "/";
}
query = evhttp_uri_get_query(http_uri);
if (query == NULL) {
sprintf(uri, "%s", path);
}
else {
sprintf(uri, "%s?%s", path, query);
}
uri[sizeof(uri)-1] = '\0';
// Create event base
base = event_base_new();
if (!base) {
perror("event_base_new()");
goto error;
}
// For simplicity, we let DNS resolution block. Everything else should be
// asynchronous though.
//evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port);
evcon = evhttp_connection_base_new(base, NULL, host, port);
if (evcon == NULL) {
fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
goto error;
}
if (retries > 0) {
evhttp_connection_set_retries(evcon, retries);
}
if (timeout >= 0) {
evhttp_connection_set_timeout(evcon, timeout);
}
//bev = evhttp_connection_get_bufferevent(evcon);
//if (bev == NULL) {
// printf("evhttp_connection_get_bufferevent() failed\n");
// goto error;
// }
// Fire off the request
req = evhttp_request_new(http_request_file_done, bev);
if (req == NULL) {
fprintf(stderr, "evhttp_request_new() failed\n");
goto error;
}
char tmp_buf[256] = { 0 };
output_headers = evhttp_request_get_output_headers(req);
evhttp_add_header(output_headers, "Host", host);
evhttp_add_header(output_headers, "Connection", "keep-alive");
r = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, uri);
if (r != 0) {
fprintf(stderr, "evhttp_make_request() failed\n");
goto error;
}
event_base_dispatch(base);
goto cleanup;
error:
ret = 1;
cleanup:
if (evcon)
evhttp_connection_free(evcon);
if (http_uri)
evhttp_uri_free(http_uri);
event_base_free(base);
return ret;
}
int main( int c, char **v )
{
int idx;
int reverse = 0, servertest = 0, use_getaddrinfo = 0;
struct event_base *event_base = NULL;
struct evdns_base *evdns_base = NULL;
if( c < 2 ){
fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
return 1;
}
idx = 1;
while (idx < c && v[idx][0] == '-') {
if (!strcmp(v[idx], "-x"))
reverse = 1;
else if (!strcmp(v[idx], "-v"))
verbose = 1;
else if (!strcmp(v[idx], "-g"))
use_getaddrinfo = 1;
else if (!strcmp(v[idx], "-servertest"))
servertest = 1;
else
fprintf(stderr, "Unknown option %s\n", v[idx]);
++idx;
}
#ifdef WIN32
{
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
}
#endif
http_get_file("http://172.16.1.18:8080/test.bin", "");
//http_get_(REQ_TEST0);
//fflush(stdout);
printf("debug4\n" );
return 0;
}
#endif
阅读全文
0 0
- libevent post下载文件
- post方式下载文件
- post方式下载文件
- Post请求下载文件
- POST文件上传和下载
- 以POST方式下载文件
- 发送post请求下载文件
- libevent http post 服务器
- javaWeb通过POST来下载文件
- post方式下载文件ie8不支持
- js发送post请求下载文件
- 使用js发送post参数下载文件
- AngularJS通过post方法下载excel文件
- 用post或者get实现文件下载
- post方式下载后台传来的文件
- libevent http client post get
- libevent http post json 测试
- post请求,下载excl文件(文件由poi生成)
- 4143:和为给定数
- gitbook 生成 html 电子书, 搭建在 github 或 本地服务器上
- java-(继承、子父类中的变量的特点、子父类中函数的特点-覆盖、)
- 51nod 1640 天气晴朗的魔法 【二分枚举最大生成树】or【最小&&最大 生成树】
- Codeforces828 A. Restaurant Tables
- libevent post下载文件
- IOS常用框架
- 动态规划(一)入门例子
- kaggle竞赛——Titanic:Machine Learning from Disaster
- MOOC清华《程序设计基础》第5章:N皇后问题(带矩阵输出)
- waiting for headers 等待时间过久
- JavaScript的正则表达式(二)
- 剖析tinyhttp源码
- Sa总结