尝试接收网站的http响应消息时遇到一些问题。
这是我的函数:在C中使用OpenSSL接收http响应消息
void Reveive_response(char *resp, SSL *ssl) {
const int BUFFER_SIZE = 1024;
char response[1048576];
char *buffer = NULL; // to read from ssl
char *check = (char *) malloc(BUFFER_SIZE*sizeof(char));
int bytes; // number of bytes actually read
int received = 0; // number of bytes received
buffer = (char *) malloc(BUFFER_SIZE*sizeof(char)); // malloc
memset(response, '\0', sizeof(response)); // response
assign = '\0'
do{
memset(buffer, '\0', BUFFER_SIZE); // empty buffer
bytes = SSL_read(ssl, buffer, BUFFER_SIZE);
if (bytes < 0) {
printf("Error: Receive response\n");
exit(0);
}
if (bytes == 0) break;
received += bytes;
printf("Received...%d bytes\n", received);
strncat(response, buffer, bytes); // concat buffer to response
} while (SSL_pending(ssl)); // while pending
response[received] = '\0';
printf("Receive DONE\n");
printf("Response: \n%s\n", response);
free(buffer);
strcpy(resp, response); // return via resp
}
当我调用该函数,这似乎是响应消息是不完整的。就像这样:
Received...1014 bytes
Received...1071 bytes
Receive DONE
Response:
HTTP/1.1 200 OK
<... something else....>
Vary: Accept-Encoding
Content-Type: text/html
Conne
那么,如果我再次调用该函数,它返回:
Received...39 bytes
Receive DONE
Response:
ction: keep-alive
Content-Length: 0
中的现场连接被分裂。为什么我的功能没有收到所有的回复信息?我用while循环里面。请告诉我我哪里错了?谢谢。
谢谢。我会尽快回复你。 – thanhdx
嗨,阅读更多信息后,我仍然有一些问题: – thanhdx
- 如何阅读一行文字直到CRLF? SSL_read()按字节读取缓冲区。我应该读取字节缓冲区然后解析它来处理每一行?或者逐个读取字节直到CRLF(它可能运行得很慢)?还是其他什么? – thanhdx