struct sockaddr_in web; /* 服务器 */ char request[1024] = {'\0'}; /* 客户端发送的请求报文内容 */ char recvbuf[4096] = {'\0'}; /* 客户端接收的内容 */ char sessionid[40] = {'\0'}; /* 获取JSESSIONID */ FILE *fd; memset(&web, 0, sizeof(web)); web.sin_family = AF_INET; /* ipv4 */ web.sin_port = ntohs(PORT); web.sin_addr.s_addr = inet_addr(SERVER_IP);
s = socket(PF_INET, SOCK_STREAM, 0); /* socket */ if (s == -1) bail("socket(1)"); /* 错误处理函数 */ c = connect(s, (struct sockaddr *)&web, sizeof(web)); if (c == -1) bail("connect(1)");
s = socket(PF_INET, SOCK_STREAM, 0); c = connect(s, (struct sockaddr *)&web, sizeof(web)); memset(request, 0, 2048); strcat(request, "POST /check/checkuser.jsp HTTP/1.1\r\n"); strcat(request, "Accept: text/html\r\n"); strcat(request, "Referer: "); strcat(request, "Accept-Language: zh-cn\r\n"); strcat(request, "Content-Type: application/x-www-form-urlencoded\r\n"); strcat(request, "Accept-Encoding: gzip, deflate\r\n"); strcat(request, "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon)\r\n"); strcat(request, "Host: 59.xx.xx.xx:xx\r\n"); strcat(request, "Content-Length: 63\r\n"); strcat(request, "Connection: Keep-Alive\r\n"); strcat(request, "Cache-Control: no-cache\r\n\r\n"); strcat(request, "username=liuby&userpw=MIMA&usertype=student&Submit=%B5%C7%C2%BD"); // printf("%s\n,%d\n", request,strlen(request)); z = send(s, request, strlen(request), 0); /* send */
recv(s, recvbuf, 4096, 0); /* 接收 */
get_sessionid(recvbuf); /* 获取sessionid */ /* 此时接收到的报文显示为302 Moved Temporarily,这种一般需要再次发送请求GET需要跳转到的网页,不知道我这样理解对不对,完全是根据这次做的来猜测的 */ memset(request, 0, 2048); strcat(request, "GET /check/students/stulogin1.jsp?username=liuby&usertype=student HTTP/1.1\r\n")");
/* 根据抓包分析和JSESSIONID,再次构造要发送到服务器的报文 */
close(s); /* 关闭连接,如果要再次请求,重新建立socket,connect,send等 */
|