Thread: Sending http get request

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    Sending http get request

    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? 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();
     
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    I do not think you will want to be using SOCK_STREAM. This is most likely a job for SOCK_RAW. With SOCK_STREAM some other process must be listening on the said port and establish a connection with your application. SOCK_RAW allows you to craft custom packets.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You're connecting, but you're not reading from the socket after the connect. You're on the right track though.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    oops
    Last edited by CommonTater; 09-07-2010 at 08:58 PM. Reason: I'm just too tired....

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    ok thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP GET request
    By aosmith in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 07:00 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  4. Sending an http POST request
    By jaxen in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2006, 12:35 PM
  5. Sending http request to httpd
    By Encrypted in forum C Programming
    Replies: 3
    Last Post: 03-30-2003, 04:57 AM