title: http request using sockets on c++
body: <p>I'm trying to do an HTTP request using sockets on Linux, and my function works now, but only with simple domains as <code>www.google.com</code> or <code>webpage.000webhostapp.com</code>. When I try to add a path and query like <code>webpage.000webhostapp.com/folder/file.php?parameter=value</code>, it starts failing.</p>
  <p>This is my code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;string&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;stdio.h&gt;
  
  int main(){
   int socket_desc;
   struct sockaddr_in serv_addr;
   struct hostent *server;
   char buffer[4096];
  
   std::string url = &quot;www.exampleweb.com/folder/file.php&quot;;
  
   socket_desc = socket(AF_INET, SOCK_STREAM, 0);
   if(socket_desc &lt; 0){
    std::cout&lt;&lt;&quot;failed to create socket&quot;&lt;&lt;std::endl;
   }
  
   server = gethostbyname(url.c_str());
   if(server==NULL){std::cout&lt;&lt;&quot;could Not resolve hostname :(&quot;&lt;&lt;std::endl;}
   bzero((char *) &amp;serv_addr, sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_port = htons(80);
   bcopy((char *)server-&gt;h_addr,
           (char *)&amp;serv_addr.sin_addr.s_addr,
           server-&gt;h_length);
  
   if(connect(socket_desc, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0){
     std::cout&lt;&lt;&quot;connection failed :(&quot;&lt;&lt;std::endl;
   }
   std::string request = &quot;GET / HTTP/1.1\r\nHost: &quot; + url + &quot;\r\nConnection: close\r\n\r\n&quot;;
  
   if(send(socket_desc, request.c_str(), strlen(request.c_str())+1, 0) &lt; 0){
    std::cout&lt;&lt;&quot;failed to send request...&quot;&lt;&lt;std::endl;
   }
   int n;
   std::string raw_site;
   while((n = recv(socket_desc, buffer, sizeof(buffer)+1, 0)) &gt; 0){
    int i = 0;
          while (buffer[i] &gt;= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
  
              raw_site+=buffer[i];
              i += 1;
          }
   }
  
   std::cout&lt;&lt;raw_site&lt;&lt;std::endl;
   return 0;
  }
  </code></pre>
  <p>Using <code>www.google.com</code>, it works perfectly for the request.</p>
  <p>Using <code>www.example.com/folder/file.php?parameter=value&amp;parameter2=value2</code>, it fails and returns <code>&quot;could not resolve hostname&quot;</code>.</p>
  <p>Any idea on how to fix it?</p>
  <p>I'm not using curl and can't use it for this project.</p>