winsock 实现HTTP GET/POST

2021-05-04 06:28

阅读:369

标签:send   net   can   +=   art   winsock   cti   substr   family   

#include 
#include 
#include 

#include "winsock2.h"
#pragma comment(lib,"WS2_32.lib")

int socket_readline(SOCKET socket,char *head) 
{ 
    char c = ‘\0‘; 
    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] = ‘\0‘; 
    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);
	protocal = protocal==5?protocal=443:protocal=80;

	char *hostPort = p;
	char *hostPortEnd = strstr(p,"/");
	if(!hostPortEnd) {
		if(strlen(p) 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] = ‘\0‘;

        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] = ‘\0‘;
    }

	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] = ‘\0‘;
		
        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] = ‘\0‘;
    }
	
	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 实现HTTP GET/POST

标签:send   net   can   +=   art   winsock   cti   substr   family   

原文地址:http://www.cnblogs.com/hatsusakana/p/7724691.html


评论


亲,登录后才可以留言!