用C语言向服务器发送HTTP请求后什么也没收到

时间:2018-11-14 14:38:39

标签: c sockets http

我想用C语言创建一个小的下载器。一切都很好,直到我想获取网页的内容。每次答案都是空的,因为什么都不显示。我不明白为什么。

由于使用了正则表达式,我获得了主机,端口号和路径,但是我没有显示它,因为SO表示我的帖子主要是代码...

#define BUF_SIZE 100

int main (int argc, char *argv[] )
{
  int s;
  int response;
  struct sockaddr_in sin; 
  struct hostent *hptr;

  char * regexHost = "^https?://([^:0-9/]*):?([0-9]*)?(.*)$";
  size_t maxGroups = 4;

  char * host;
  char * port;
  char * path;
  char *request = malloc(256);
  char buf[BUF_SIZE];

  /* Creating socket */
  printf("Creating socket...\n");
  if ( (s = socket(PF_INET, SOCK_STREAM, 0 ) ) < 0) /* create socket*/
    { 
      perror("error on socket\n");  /* socket error */
      return -1;
    }
  else
    {
        printf("Successfully created socket\n");
    }

  sin.sin_family = PF_INET; /*set protocol family to Internet */

  /*Getting host*/
  printf("Getting host...\n");
  if ( (hptr =  gethostbyname(host) ) == NULL)
    {
      fprintf(stderr, "gethostname error: %s\n", host);
      return -1;
    }
  else
    {
        printf("Successfully got host\n");
    }

  /* Connect to the server */
  printf("Connecting...\n");
  memcpy( &sin.sin_addr, hptr->h_addr, hptr->h_length);
  if (connect (s, (struct sockaddr *)&sin, sizeof(sin) ) < 0 ) 
    {
      perror("error on connect\n"); return -1;   /* connect error */
    }
  else
    {
        printf("Successfully connected\n");
    }

  /* send headers to connection*/
  sprintf(request, ("GET %s HTTP/1.1\r\nHost: %s\r\n"), path, host);
  printf("%s\n", request);

  if( send(s, request, strlen(request), 0) < 0)
    {
      puts("Send failed");
      return 1;
    }
   else
    {
      printf("Successfully sent HTTP fetch request\n");
    }

  while ((response = recv(s, buf, BUF_SIZE-1, 0)) > 0) 
    {
      buf[response] = 0;
      printf("Response : %s\n", buf);
      fputs(buf, stdout);
      memset(buf, 0, BUF_SIZE);
    }

  /* close connection, clean up socket */
  if (close(s) < 0) 
    { 
      perror("error on close");   /* close error */
      return -1;
    }
  return 0;
}

1 个答案:

答案 0 :(得分:4)

根据RFC 7230

  

所有HTTP / 1.1消息均包含一个起始行,后跟一个   八位字节序列,格式类似于Internet消息   格式[RFC5322]:零个或多个标头字段(统称为   作为“标题”或“标题部分”),空行   指示标题部分的末尾,以及可选消息   身体。

 HTTP-message   = start-line
                  *( header-field CRLF )
                  CRLF
                  [ message-body ]

请注意,在起始行(附加内容)CRLF之后,后面还有另一个CRLF,以指示此处是标题的结尾。

因此,在构建它的调用"\r\n"的调用中,在请求字符串的末尾添加另一个sprintf