1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Sending http get request

Discussion in 'C' started by LeeVerr, Sep 8, 2010.

  1. LeeVerr

    LeeVerr New Member

    Joined:
    Sep 8, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi all, just wondering if this would be the correct way to send a http get request, or if it's not safe, very error prone? :p Dont need a response from the server

    Code:
    void httpreq(char *IPadd)
    {
        char *request = "";
    
        struct sockaddr_in serveraddr;
        int sock;
    
        WSADATA wsaData;
        char *ipaddress = IPadd;
        int port = 80;
    
        request = "GET / HTTP/1.1\r\n\r\n";
    
        //init winsock
        WSAStartup(MAKEWORD(2, 0), &wsaData);
    
        //open socket
        if(sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)!=-1) {
                    
                    memset(&serveraddr, 0, sizeof(serveraddr));
                    serveraddr.sin_family      = AF_INET;
                    serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
                    serveraddr.sin_port        = htons((unsigned short) port);
                    
                    if(connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr))!=-1) { 
                            //send request
                            send(sock, request, strlen(request), 0);
                    }
            }
    
        
        closesocket(sock);
    
        //cleanup
        WSACleanup();
     
    }
     

Share This Page