简体   繁体   中英

How to make http request and send data another server in APACHE2.2

I am developing one apache plugin which will fetch some data from request header and need to send the data another server. I tried apr_socket_sento() and apr_socket_create() but none of them are working . Please tell me how to send or get data from another server in apache2.2 plug in .

Please find the ans how to send and receive data from the server.

char* post_data_server_sync(request_rec *r,char *host,char *payload,int timout)
{
char buffer[1024];
char *hostname=host;
char *final_msg;
char *msg = "POST /getRequestData HTTP/1.1\r\n"\
            "HOST: %s\r\n" \
            "Accept: */*\r\n"\
            "Connection: keep-alive\r\n"\
            "Content-Length: %d\r\n"\
            "Content-Type: application/x-www-form-urlencoded\r\n\n%s";
apr_socket_t *new_sock;
apr_sockaddr_t *addr;
apr_port_t port_no=80;
apr_status_t result;
apr_interval_time_t tmout=timout;
apr_size_t *size=apr_palloc(r->pool,sizeof(apr_size_t));
apr_size_t *size_rcv=apr_palloc(r->pool,sizeof(apr_size_t));
final_msg=apr_psprintf(r->pool,msg,host,strlen(payload),payload);
result=apr_socket_create(& new_sock,APR_INET,SOCK_STREAM,APR_PROTO_TCP,r->pool);
result=apr_sockaddr_info_get(&addr,hostname,APR_INET,port_no,0,r->pool);
result=apr_socket_connect(new_sock,addr);
*size=strlen(final_msg);
result=apr_socket_sendto(new_sock,addr,0,final_msg,size);
if(result==APR_SUCCESS)
{
    *size_rcv=sizeof(buffer);
   result=apr_socket_recvfrom(addr,new_sock,0,buffer,size_rcv);
}
result=apr_socket_close(new_sock);
return buffer;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM