1

我正在尝试使用套接字并尝试构建一个非常简单的 webbot。

这是我的代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <cstring>
#include <iostream>
#include <string>

#define HTTP_PORT "80"

#define HOST "www.taringa.net"
#define PORT HTTP_PORT

#define IN_BUFFSIZE 1024
#define OUT_BUFFSIZE 1024

#define REQUEST "GET /Taringa/posts HTTP/1.0\r\nHost: www.taringa.net\r\nUser-Agent: foo\r\n\r\n"

using namespace std;

int main(int argc, char **argv) {

  struct addrinfo hints, *res;
  struct sockaddr_in servAddress;
  int sockfd;

  char addrstr[100];
  char buff_msg_out[OUT_BUFFSIZE], buff_msg_in[IN_BUFFSIZE];

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ( getaddrinfo(HOST, HTTP_PORT, &hints, &res) != 0) {
     cerr << "Error en getaddrinfo" << endl;
     return -1;
  }


  // Crear socket
  if ( ( sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol) ) < 0 )
  {
   cerr << "Error en socket()" << endl;
   return -1;
  }


  // Iniciar conexion
  if ( connect(sockfd, res->ai_addr, res->ai_addrlen) == -1 )
  {
   cerr << "Error en connect()" << endl;
   cerr << "Error: " << strerror(errno) << endl;
   return -1;
  }

  cout << "Conectado con éxito" << endl;

  // Enviar datos
  strncpy(buff_msg_out, REQUEST, strlen(REQUEST));

  if ( send(sockfd, buff_msg_out, strlen(buff_msg_out), 0) <= 0 )
  {
   cerr << "Error en write()" << endl;
   return -1;
  }

  cout << "Mensaje enviado:" << endl << buff_msg_out << endl << endl;

  int bytes_recv = 0;

  while ( ( bytes_recv = recv(sockfd, buff_msg_in, IN_BUFFSIZE-1, 0)) > 0 )
  {
    buff_msg_in[bytes_recv] = '\0';
    cout << buff_msg_in << endl;        
  }

  freeaddrinfo(res);
  close(sockfd);

  return 0;
}

这是请求具有 HTTP/1.0 时的输出

HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 19:57:26 GMT
Content-Type: text/html; charset=utf8
Connection: close
Set-Cookie: trngssn=06359673; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf/profile" prefix="og: http://ogp.me/ns
# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Temas de Taringa" href="/rss/Taringa/tem
as/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

    </body>
</html>

但是当我指定 HTTP/1.1 时,回复是

HTTP/1.1 200 OK
Server: n0
Date: Fri, 01 Feb 2013 20:03:54 GMT
Content-Type: text/html; charset=utf8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: trngssn=81047255; path=/
Set-Cookie: trngssn=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: taringa_user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: lastNick=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: fbs=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: tws=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net
Set-Cookie: iB-friendfind=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.taringa.net

d0f
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="es" xml:lang="es" >
        <head profile="http://purl.org/NET/erdf
/profile" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv=”X-Frame-Options” content=”Deny” />

                <link rel="alternate" type="application/atom+xml" title="Últimos Posts de Taringa" href="/rss/Taringa/posts/" />
                <link rel="alternate" type="application/atom+xml" title="Últimos Te
mas de Taringa" href="/rss/Taringa/temas/" />

                <title>Posts de Taringa!  - Taringa!</title>

...

这就是问题所在

    </body>
</html>

0

并在关闭通信之前等待几秒钟。

我刚刚尝试使用 stackoverflow.com/about,它工作正常。网页后服务器发给我的以下文字除外

</html>HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>

我错过了什么吗?

4

2 回答 2

5

服务器正在使用分块编码。d0f 是以八位字节表示的“块”的长度,以十六进制表示。0 是下一个块的长度(即没有)。

于 2013-02-01T20:32:20.423 回答
3

服务器:n0
日期:2013 年 2 月 1 日星期五 20:03:54 GMT
内容类型:text/html;charset=utf8
传输编码:分块
连接:保持活动

如果您不支持,请不要说您支持 HTTP 1.1。

所有 HTTP/1.1 应用程序必须能够接收和解码“分块”传输编码,并且必须忽略他们不理解的块扩展扩展。-- HTTP 1.1

于 2013-02-01T20:30:57.463 回答