#include <stdio.h> #include <stdlib.h> #include <string.h> #include "winsock2.h" #pragma comment (lib, "Ws2_32.lib") int socket_readline (socket Socket,char *head) {char c = ' + '; int r = 0,i = 0; while (recv (socket,&c,1,0) >0) {if (R = = 1) {if (c = = ' \ n ') { Break } else {r = 0; }} else if (c = = ' \ r ') {r = 1; } else head[i++] = c; } Head[i] = ' + '; if (strcmp (head, "\ r \ n") = = 0) return-1; else return i; } int Parse_url (char *url,char *host,int *port,char *path,char *data) {char *p = strstr (URL, "://"); if (!p) return-1;p + = 3; int protocal = (int) (p-url);p rotocal = Protocal==5?protocal=443:protocal=80;char *hostport = P;char *hostPortEnd = strstr (P, "/"); if (!hostportend) {if (strlen (p) < 1) Return-1;else hostportend = P+strlen (p);} Char *hostend = strstr (Hostport, ":"); if (!hostend) {*port = Protocal;hostend = Hostportend;} Else{*port = (int) atoi (hostend+1);} strncpy (host,hostport,hostend-hostport); char *pathstart = hostportend;if (!pathstart) Return-1;char *pathEnd = Strstr ( Pathstart, "?"); if (!pathend) strncpy (Path,pathstart,strlen (Pathstart)); elsestrncpy (Path,pathstart,pathend-pathstart); if (pathEnd ) strncpy (Data,pathend+1,strlen (pathend)-1); return 1;} Const char* Strstri (const char* STR, const char* SUBSTR) {int len = strlen (SUBSTR); if (len = = 0) {return NULL;} while (*STR) {if (strncmp (str, subStr, len) = = 0) {return str;} ++str;} return NULL;} Char *ajax_get (char *url) {int port = 0;char Host[256],path[256],data[256];memset (host,0,sizeof (host)); Memset (path,0, sizeof (path); memset (data,0,sizeof (data)); if (!parse_url (url,host,&port,path,data)) return NULL; Socket s = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP), if (s = = Invalid_socket) {return NULL;} sockaddr_in servaddr;servaddr.sin_family = Af_inet;servaddr.sin_port = htons (port); Servaddr.sin_addr. S_un. S_ADDR = inet_addr (Host), connect (s, (sockaddr*) &servaddr,sizeof (SERVADDR)), char *getformat = "GET%s?%s http/1.1\r\n" "Host:%s\r\n" "connection:keep-alive\r\n" "\ r \ n"; int senddatasize = strlen (getFormat) +strlen (path) +strlen (data) +strlen (host) +200 ; Char *senddata = (char *) malloc (senddatasize); sprintf (senddata,getformat,path,data,host); Send (S,senddata,strlen ( SendData), 0); free (senddata); char *http_head_recv = new char[3000]; int content_length = 0; for (int flag=1;flag>0;) {memset (http_head_recv,0,sizeof (HTTP_HEAD_RECV)); Flag = Socket_readline (S,HTTP_HEAD_RECV); char key[125],value[2000]; memset (key,0,sizeof (key)); memset (value,0,sizeof (value)); sscanf (Http_head_recv, "%s%s", Key,value); Key[strlen (key)-1] = ' + '; if (Strstri (Key, "content-length") = NULL) {content_length = atoi (value); }} free (HTTP_HEAD_RECV), char *msg = null;if (content_length>0) {msg = new char[content_length+1]; memset (msg,0,content_length+1); Recv (s,msg,content_length,0); Msg[content_length] = ' + '; }closesocket (s); return msg;} Char *ajax_post (char *url) {int port = 0;char Host[256],path[256],data[256];memset (host,0,sizeof (host)); Memset (path,0 , sizeof (path)); memset (data,0,sizeof (data)); if (!parse_url (url,host,&port,path,data)) return NULL; Socket s = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP), if (s = = Invalid_socket) {return NULL;} sockaddr_in servaddr;servaddr.sin_family = Af_inet;servaddr.sin_port = htons (port); Servaddr.sin_addr. S_un. S_ADDR = inet_addr (host), connect (s, (sockaddr*) &servaddr,sizeof (SERVADDR)), char *getformat = "POST%s http/1.1\r\n "Host:%s\r\n" "connection:keep-alive\r\n" "\r\n%s"; int senddatasize = strlen (getFormat) +strlen (path) +strlen (data ) +strlen (host) +200;char *senddata = (char *) malloc (senddatasize); sprintf (senddata,getformat,path,host,data); Send (S , Senddata,strlen (SendData), 0); free (senddata); char *http_head_recv = new char[3000]; int content_length = 0; for (int flag=1;flag>0;) {memset (http_head_recv,0,sizeof (HTTP_HEAD_RECV)); Flag = Socket_readline (S,HTTP_HEAD_RECV); char key[125],value[2000]; memset (key,0,sizeof (key)); memset (value,0,sizeof (value)); sscanf (Http_head_recv, "%s%s", Key,value); Key[strlen (key)-1] = ' + '; if (Strstri (Key, "content-length") = NULL) {content_length = atoi (value); }} free (HTTP_HEAD_RECV), char *msg = null;if (content_length>0) {msg = new char[content_length+1]; memset (msg,0,content_length+1); Recv (s,msg,content_length,0); Msg[content_length] = ' + '; }closesocket (s); return msg;} int main (int argc, char* argv[]) {char *myajax_test = "Http://10.10.22.102/myajax/test.php?user=admin"; Wsadata Wsadata; WORD sockversion = Makeword (2,0); WSAStartup (Sockversion, &wsadata), char *text = Ajax_post (myajax_test), if (text) {printf ("%s\n", text);} WSACleanup (); return 0;}
Winsock implements HTTP Get/post