C语言模拟web进行post数据提交

#include #include #include #include #include #include #include #include #include #include #include #define SERVER "www.su.zju.edu.cn" #define PORT 80 #define THREAD_NUM 5 pthread_mutex_t sock_mutex; pthread_cond_t sock_cond; char* gen_request() { char* text = (char*)malloc(sizeof(char) * 2048); memset(text, 0, sizeof(char) * 2048); /* text packet get from wireshark */ strcat(text, "POST /hack/vote.php?type=2 HTTP/1.1/r/n"); strcat(text, "Host: [url]www.su.zju.edu.cn[/url]/r/n"); strcat(text, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)/r/n"); strcat(text, "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5/r/n"); strcat(text, "Accept-Language: zh-cn,zh;q=0.5/r/n"); strcat(text, "Accept-Encoding: gzip,deflate/r/n"); strcat(text, "Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7/r/n"); strcat(text, "Keep-Alive: 300/r/n"); strcat(text, "Connection: keep-alive/r/n"); strcat(text, "Referer: [url]http://www.su.zju.edu.cn/[/url]/r/n"); strcat(text, "Cookie: usrtime=1178023651; lasturl=http%3A%2F%2Fwww.su.zju.edu.cn%2Fhack%2Fvisit.php%3Ftype%3D1; loginurl=http%3A%2F%2Fwww.su.zju.edu.cn%2Fhack%2Fvote.php%3Ftype%3D2; usrsid=XgVB1KO7mVEanJwLbwJ0lqXBSHErTAcN; usripfrom=Unknow; usrtime=1178023650; lasturl=http%3A%2F%2Fwww.su.zju.edu.cn%2F/r/n"); strcat(text, "Content-Type: application/x-www-form-urlencoded/r/n"); strcat(text, "Content-Length: 10/r/n"); strcat(text, "/r/n"); strcat(text, "voteid=276"); return text; } void* sock_thread(void* data) { while(1) { /* wait to start vote */ pthread_mutex_lock(&sock_mutex); pthread_cond_wait(&sock_cond, &sock_mutex); pthread_mutex_unlock(&sock_mutex); struct hostent* hostinfo; struct sockaddr_in name; name.sin_family = AF_INET; name.sin_port = htons(PORT); hostinfo = gethostbyname(SERVER); if(hostinfo == NULL) continue; name.sin_addr = *(struct in_addr*)hostinfo->h_addr; int sock = socket(PF_INET, SOCK_STREAM, 0); if(sock < 0) continue; /* connect to the server */ if(connect(sock, (struct sockaddr*)&name, sizeof(name)) < 0) continue; char* text = gen_request(); /* send it !!! */ write(sock, text, strlen(text) + 1); memset(text, 0, sizeof(char) * 2048); free(text); /* well... better receive it */ text = (char*)malloc(sizeof(char) * 1024); while(1) { int nbyte = recv(sock, text, 1024, 0); if(nbyte < 1024) break; } free(text); /* close the connection */ close(sock); } } int main(int argc, char* argv[]) { /* initialize */ pthread_t* arr_pid = (pthread_t*)malloc(sizeof(pthread_t) * THREAD_NUM); pthread_mutex_init(&sock_mutex, NULL); pthread_cond_init(&sock_cond, NULL); /* create all the thread */ int i; for(i = 0; i < THREAD_NUM; i++) pthread_create(&arr_pid[i], NULL, sock_thread, NULL); while(1) { /* sleep for a while to let one of the thread to go */ usleep(200); pthread_cond_signal(&sock_cond); } /* clean up all the thread */ for(i = 0; i < THREAD_NUM; i++) pthread_join(arr_pid[i], NULL); return 0; }

你可能感兴趣的:(C语言模拟web进行post数据提交)