libevent 上传文件

print_log("evhttp_connection_get_bufferevent() ok\n"); // Fire off the request req = evhttp_request_new(http_post_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"); print_log("%s", data_file); FILE * f = fopen(data_file, "rb"); char buf[1024]; size_t s; size_t bytes = 0; if (!f) { syntax_(); goto error; } output_buffer = evhttp_request_get_output_buffer(req); char head_buf[256]; int tmplen = strlen("------WebKitFormBoundaryAnNdXIAa997m6AMZ\r\n"); evbuffer_add(output_buffer, "------WebKitFormBoundaryAnNdXIAa997m6AMZ\r\n", strlen("------WebKitFormBoundaryAnNdXIAa997m6AMZ\r\n")); sprintf(head_buf, "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n", _filename); tmplen += strlen(head_buf); evbuffer_add(output_buffer, head_buf, strlen(head_buf)); sprintf(head_buf, "Content-Type: application/octet-stream\r\n\r\n"); tmplen += strlen(head_buf); evbuffer_add(output_buffer, head_buf, strlen(head_buf)); while ((s = fread(buf, 1, sizeof(buf), f)) > 0) { evbuffer_add(output_buffer, buf, s); bytes += s; } sprintf(head_buf, "\r\n------WebKitFormBoundaryAnNdXIAa997m6AMZ--\r\n"); tmplen += strlen(head_buf); evbuffer_add(output_buffer, head_buf, strlen(head_buf)); evutil_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long)bytes + tmplen); evhttp_add_header(output_headers, "Content-Length", buf); evhttp_add_header(output_headers, "Cache - Control", "max - age = 0"); //sprintf(tmp_buf, "http://%s", host); //evhttp_add_header(output_headers, "Origin", tmp_buf); //evhttp_add_header(output_headers, "Origin", "null"); evhttp_add_header(output_headers, "Upgrade - Insecure - Requests", "1"); //evhttp_add_header(output_headers, "User - Agent", "Mozilla / 5.0 (Windows NT 6.1; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 56.0.2924.87 Safari / 537.36"); sprintf(tmp_buf, "multipart/form-data;boundary=%s", "----WebKitFormBoundaryAnNdXIAa997m6AMZ"); evhttp_add_header(output_headers, "Content-Type", tmp_buf); evhttp_add_header(output_headers, "Accept", "text / html, application / xhtml + xml, application / xml; q = 0.9, image / webp, */*;q=0.8"); evhttp_add_header(output_headers, "Accept-Encoding", "gzip, deflate, br"); evhttp_add_header(output_headers, "Accept-Language", "zh-CN,zh;q=0.8"); fclose(f); r = evhttp_make_request(evcon, req, EVHTTP_REQ_POST, 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; } #endif
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。