static int ghttp_post_data_buff(ghttp_request *request, struct ghttp_post_data *data) { unsigned int post_len = 0; if(!request || !data || !data->buff || !data->bytes) return -1; if(!data->loop){ return 0; } post_len = data->bytes - data->post_bytes; if(post_len == 0) return 0; else if(post_len > CONN_IO_BUF_CHUNKSIZE_DEFAULT) post_len = CONN_IO_BUF_CHUNKSIZE_DEFAULT; ghttp_set_body(request, (char *)data->buff + data->post_bytes, post_len); data->post_bytes += post_len; if(data->post_bytes == data->bytes){ data->post_bytes = 0; --data->loop; data->post_total_bytes += data->bytes; } return 0; }
static int ghttp_post_data_file(ghttp_request *request, struct ghttp_post_data *data) { size_t r_read; if(!request || !data || !data->buff || !data->bytes) return -1; if(!data->loop){ if(data->fp){ fclose(data->fp); data->fp = NULL; } return 0; } while(1){ r_read = fread(ghttp_chunk, 1, CONN_IO_BUF_CHUNKSIZE_DEFAULT, data->fp); if(r_read >= 0) break; else if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) continue; else return -1; } if(r_read > 0){ ghttp_set_body(request, (char *)ghttp_chunk, r_read); data->post_bytes += r_read; if(data->post_bytes == data->bytes){ data->post_bytes = 0; --data->loop; data->post_total_bytes += data->bytes; fseek(data->fp, 0, SEEK_SET); } } else ghttp_set_body(request, NULL, 0); return 0; }
void main(int argc,char *argv[]) { ghttp_request *request = NULL; request = ghttp_request_new(); ghttp_set_uri(request, argv[1]); ghttp_set_type(request, ghttp_type_post); ghttp_set_body(request, argv[2],strlen(argv[2])); printf("%s\n", argv[2]); ghttp_prepare(request); ghttp_process(request); fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout); fprintf(stdout, " Length: %s\n", ghttp_get_header(request, "Content-Length")); fprintf(stdout, " Location: %s\n", ghttp_get_header(request, "Location")); }
static void http_request(int req_type) { char *str; struct json_object *json,*body; json = json_object_new_object(); json_object_object_add(json, REQ_TYPE, json_object_new_int(req_type)); if(req_type==REQ_REGISTER) { body=my_object; } else { body=json_object_new_object(); if(req_type==REQ_SUBMIT) { int ms_async; (*p_order_id)++; ms_async=msync((void*)p_order_id,(size_t)sizeof(unsigned int),MS_ASYNC); if(ms_async==-1) perror("MSYNC_ERROR"); json_object_object_add(body, AMOUNT, json_object_new_string(amount)); } json_object_object_add(body, MACHINE_ID, json_object_new_int(machine_id)); json_object_object_add(body, ORDER_ID, json_object_new_int(*p_order_id)); } json_object_object_add(json, BODY, body); str=json_object_to_json_string(json); ghttp_set_body(request, str, strlen(str)); http_status=ghttp_process(request); fd_net=ghttp_get_socket(request); // json_object_put(body); json_object_put(json); }
int post_http_request(char * url, char * json, int len, char * result, int result_len){ int64 post_begin = get_tick_count(); ghttp_request * request = ghttp_request_new(); if(!request){ log_out(log_error, "http_post::ghttp_request_new fail\n"); return -1; } if(ghttp_set_uri(request, (char*)url) == -1){ log_out(log_error, "http_post::ghttp_set_uri fail(%s)\n", ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } if(ghttp_set_type(request, ghttp_type_post) == -1) { log_out(log_error, "http_post::ghttp_set_type fail(%s)\n", ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } //����ͷ ghttp_set_header(request, http_hdr_Accept_Charset, "x-www-form-urlencoded"); ghttp_set_header(request, http_hdr_Connection, "keep-alive"); ghttp_set_header(request, http_hdr_Content_Type, "json"); //ghttp_set_header(request, http_hdr_Authorization, "Basic c2hlbmdhbmc6MTIzNDU2"); ghttp_set_header(request, http_hdr_Timeout, "5000"); char content_len[16] = {0}; sprintf(content_len, "%d", len); ghttp_set_header(request, http_hdr_Content_Length, content_len); if(ghttp_set_body(request, json, len) == -1){ log_out(log_error, "http_post::ghttp_set_body fail(%s)\n", ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } if(ghttp_prepare(request) < 0){ log_out(log_error, "http_post::ghttp_prepare fail(%s)\n", ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } ghttp_status req_status = ghttp_process(request); if (req_status == ghttp_error){ log_out(log_error, "http_post::ghttp_process url(%s) fail(%s)\n", url, ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } int stat_code = ghttp_status_code(request); if(stat_code != 200){ log_out(log_error, "http_post::data len(%d) status code(%d)\n", len, stat_code); ghttp_request_destroy(request); return -1; } int rsplen = ghttp_get_body_len(request); char * rspbody = ghttp_get_body(request); if((!rspbody) || (rsplen <= 1)){ log_out(log_error, "http_post::ghttp_get_body fail(%s)\n", ghttp_get_error(request)); ghttp_request_destroy(request); return -1; } if(rspbody[rsplen - 1] == '\n'){ rsplen--;} int ncopy = (result_len <= rsplen) ? result_len -1 : rsplen; if(result){ strncpy(result, rspbody, ncopy); } ghttp_request_destroy(request); uint take = (uint)(get_tick_count() - post_begin); log_out(log_debug, "http_post::data len(%d) take(%ums) response(%s)\n", len, take, result); return 0; }
ghttp_request *send_request(char *uri, int urilen, int mothod, char *path, int pathlen, char *body, int bodylen, char *responsebuf, int *responselen) { //char *uri = "http://www.solidot.org"; /* This is the http request object */ ghttp_request *request = NULL; ghttp_status status; char *buf = NULL, *allpath = NULL; int bytes_read = 0, ret = 0, allpathlen = urilen + pathlen; if(uri == NULL || urilen == 0 ) { ret = -1; LOG_PRINTF("Error: Func: %sAcclocate new empty request is NULL\n", __func__); return request; } allpath = calloc(1, allpathlen + 1); snprintf(allpath, allpathlen+1, "%s%s", uri, path); LOG_PRINTF("Info: Func: %s allpath is %s allpath len is %d uri is %s. \n", __func__, allpath, pathlen, uri); /* Allocate a new empty request object */ request = ghttp_request_new(); if (request == NULL) { ret = -1; LOG_PRINTF("Error: Func: %s new request return error.\n", __func__); return request; } else { LOG_PRINTF("Info: Func: %s Acclocate new empty request is ok. \n", __func__); } /* Set the URI for the request object */ if(ghttp_set_uri(request, allpath) == -1) { ret = -1; LOG_PRINTF("Error: Func: %s set uri return error.\n", __func__); return request; } LOG_PRINTF("Info: Func: %s set uri is ok. \n", __func__); /* Set the type for the request object */ if(ghttp_set_type(request, (ghttp_type)mothod) == -1) { ret = -1; LOG_PRINTF("Error: Func: %s set type return error.\n", __func__); return request; } LOG_PRINTF("Info: Func: %s set type is ok. \n", __func__); if(body != NULL && bodylen != 0) { LOG_PRINTF("Info: Func: %s set body is %s \n", __func__, body); ret = ghttp_set_body(request,body, bodylen); if (ret == -1) { LOG_PRINTF("Error: Func: %s set body return error.\n", __func__); return request; } else { LOG_PRINTF("Info: Func: %s set body is ok. \n", __func__); } } else { LOG_PRINTF("Info: Func: %s not set body.\n", __func__); } /* Prepare the connection */ ghttp_prepare(request); /* Process the request */ status = ghttp_process(request); if(status == ghttp_error) { ret = -1; LOG_PRINTF("Error: Func: %s ghttp_process return error.\n", __func__); return request; } /* OK, done */ LOG_PRINTF("Info: Func: %s Status code -> %d\n", __func__, ghttp_status_code(request)); /* Get the response for the request */ buf = ghttp_get_body(request); bytes_read = ghttp_get_body_len(request); if(buf != NULL && bytes_read >= 0 ) { memcpy(responsebuf, buf, *responselen); } /* Get the response for the request*/ *responselen = bytes_read; LOG_PRINTF("Info: Func: %s get_body len is %d response is: %s \n", __func__, bytes_read, buf); //printf("%s\n", buf); return request; }