GET命令只有body,标头缺失

时间:2017-05-20 06:50:21

标签: c sockets http

我知道为了跳过http请求中的标题信息,您需要使用//Student.h #ifndef STUDENT_H #define STUDENT_H #include <string> class Student { public: Student(const char * const name, int perm); int getPerm() const; const char * const getName() const; void setPerm(const int perm); void setName(const char * const name); Student(const Student &orig); ~Student(); Student & operator=(const Student &right); std::string toString() const; private: int perm; char *name; // allocated on heap }; #endif //studentRoll.h #ifndef STUDENTROLL_H #define STUDENTROLL_H #include <string> #include "student.h" class StudentRoll { public: StudentRoll(); void insertAtTail(const Student &s); std::string toString() const; StudentRoll(const StudentRoll &orig); ~StudentRoll(); StudentRoll & operator=(const StudentRoll &right); private: struct Node { Student *s; Node *next; }; Node *head; Node *tail; }; #endif //studentRoll.cpp #include <string> #include "studentRoll.h" #include <sstream> #include <iostream> StudentRoll::StudentRoll() { head = tail = NULL; } void StudentRoll::insertAtTail(const Student &s) { /* tail -> next = new Node; tail = tail -> next; tail -> s = s; tail -> next = null; */ Node* n = new Node; n -> s = new Student(s); n -> next = NULL; if (head == NULL){ head = n; } if(tail != NULL){ tail -> next = n; } n -> next = NULL; tail = n; } std::string StudentRoll::toString() const { std::ostringstream oss; oss << "["; Node *p = head; while (p){ oss << p -> s -> toString(); if(p != tail) oss << ","; p = p -> next; } oss << "]"; delete p; return oss.str(); } StudentRoll::StudentRoll(const StudentRoll &orig) { Node *p1 = 0; Node *p2 = 0; if (orig.head == NULL){ head = NULL; tail = NULL; } else{ head = new Node; head -> s = new Student(*(orig.head -> s)); std::cout << "got here"; p1 = head; p2 = orig.head -> next; } while(p2){ p1 -> next = new Node; p1 = p1 -> next; p1 -> s = new Student(*(orig.head -> s)); p2 = p2 -> next; } p1 -> next = NULL; tail = p1; delete p1; delete p2; } StudentRoll::~StudentRoll() { Node *current = head; while ( current != 0 ){ Node* next = current->next; delete current; current = next; } } StudentRoll & StudentRoll::operator =(const StudentRoll &right ) { // The next two lines are standard, and you should keep them. // They avoid problems with self-assignment where you might free up // memory before you copy from it. (e.g. x = x) if (&right == this) return (*this); // TODO... Here is where there is code missing that you need to // fill in... Node *p1 = 0; Node *p2 = 0; if (right.head == NULL){ head = NULL; tail = NULL; } else{ head = new Node; head -> s = new Student(*(right.head -> s)); p1 = head; p2 = right.head -> next; } while(p2){ p1 -> next = new Node; p1 = p1 -> next; p1 -> s = new Student(*(p2 -> s)); p2 = p2 -> next; } p1 -> next = NULL; tail = p1; delete p1; delete p2; // KEEP THE CODE BELOW THIS LINE // Overloaded = should end with this line, despite what the textbook says. return (*this); } 。我已经阅读了一些有关此问题的问题/主题,但没有任何实现对我有用。为简单起见,我将使用没有相对路径的URL(硬编码GET命令)。 我使用的网址是\r\n,我知道它的IP地址是elf.cs.pub.ro。此示例的超简化代码如下:

141.85.227.116

问题出在#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <errno.h> #define MAXLEN 500 void send_command(int sockfd, char sendbuf[], char * expected) { char recvbuf[MAXLEN]; if(send(sockfd, sendbuf, strlen(sendbuf), 0) > 0) { int data = recv(sockfd, recvbuf, MAXLEN - 1, 0); printf("%s\n", recvbuf); if(strstr(recvbuf, expected) == NULL) { exit(0); } } } int main(int argc, char * * argv) { int sockfd; struct sockaddr_in servaddr; char server_ip[20] = "141.85.227.116"; char sendbuf[MAXLEN]; char recvbuf[MAXLEN]; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Socket creation error.\n"); exit(-1); } memset( & servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(80); if (inet_aton(server_ip, & servaddr.sin_addr) <= 0) { printf("Invalid IP\n"); exit(-1); } if (connect(sockfd, (struct sockaddr * ) & servaddr, sizeof(servaddr)) < 0) { printf("Connection error\n"); exit(-1); } //If I use "GET / HTTP1.0\r\n" the connection never ends //and same for "GET / HTTP1.0"... sprintf(sendbuf,"GET / HTTP/1.0\r\n\r\n"); char expected[MAXLEN]; strcpy(expected, "2"); send_command(sockfd, sendbuf, expected); memset(recvbuf, 0, strlen(recvbuf)); int n; while ((n = recv(sockfd, recvbuf, MAXLEN - 1, 0)) > 0) { printf("%s",recvbuf); memset(recvbuf, 0, strlen(recvbuf)); } close(sockfd); return 0; } 我真的不知道为什么出现问题,即使在阅读了RFC 1945和网站的答案之后,我仍然不知道为什么标题不能打印... < / p>

L.E。 :为什么否定?我问了一个简单的问题,我提供了完整的测试代码+ printscreens。

1 个答案:

答案 0 :(得分:4)

这个问题很难理解,因为目前还不清楚到底是什么。只有在查看the previous question时才有意义。

在上一个问题中,OP在命令行上执行了以下操作(实际命令行,而不是HTTP请求):

 GET elf.cs.pub.ro HTTP/1.0

这不是OP所期望的,即向elf.cs.pub.ro发送HTTP / 1.0请求。相反,它使用GET命令(使用LWP perl库执行GET请求)并将第一个参数解释为URL,从而返回http://elf.cs.pub.ro的内容。然后它接受了下一个参数HTTP/1.0,并将其视为另一个URL,即http://HTTP/1.0。特定于用户的环境(或GET命令的细节)HTTP被视为www.http.com,即真实的URL因此是http://www.http.com/1.0。因此,本质上OP执行了两个命令(这里用GET显示,但也可以使用curl或wget或类似命令):

 GET http://elf.cs.pub.ro/ 
 GET http://www.http.com/1.0 

在此问题中显示的代码中,OP仅执行第一个请求。要做到第二步,必须为新的目标主机和URL修改程序。