在C、HTTP/1.0中使用SSL_Read()读取HTML响应
我使用C中的SSL_read()从网站读取HTML响应。我在请求消息中使用HTTP/1.0。以下是我的功能:在C、HTTP/1.0中使用SSL_Read()读取HTML响应,c,http,openssl,httpresponse,C,Http,Openssl,Httpresponse,我使用C中的SSL_read()从网站读取HTML响应。我在请求消息中使用HTTP/1.0。以下是我的功能: void Reveive_response(char *resp, SSL *ssl, int body_required) { char header[1048576]; char body[1048576]; int bytes; // number of bytes actually read int received = 0;
void Reveive_response(char *resp, SSL *ssl, int body_required) {
char header[1048576];
char body[1048576];
int bytes; // number of bytes actually read
int received = 0; // number of bytes received
int i, line_length;
char c[1];
memset(header, '\0', sizeof(header)); // header assign = '\0'
memset(body, '\0', sizeof(body)); // body assign = '\0'
/***********Try to read byte by byte***********/
i = 0;
line_length = 0; // to check length of each line
do {
bytes = SSL_read(ssl, c, 1); // read 1 byte to c[0]
if (bytes <= 0) break; // read fall or connection closed
if (c[0] == '\n') { // if '\n'
if (line_length == 0) break; // empty line, so end header
else line_length = 0; // else reset for new line
} else if ( c[0] != '\r') line_length++; // inc length
header[i++] = c[0]; // add to header
received += bytes; // count
} while (1);
fprintf(f, "%s\n", header);
//printf("Received...%d\n", received);
printf("\n##### Header DONE #####\n");
strcpy(resp, header); // return via resp
/***********************************************/
/********Then try to read body if needed********/
char *buf = malloc(1024*sizeof(char));
if (body_required) {//read body
do {
memset(buf, '\0', 1024*sizeof(char));
bytes = SSL_read(ssl, buf, 1024);
if (bytes <= 0) break;
//printf("Wait : ");
//getchar();
printf("%s", buf);
strcat(body, buf);
} while (1);
printf("\n##### Body DONE #####\n");
}
fprintf(f, "%s\n", body);
free(buf);
/***********************************************/
fprintf(f, "=============================\n");
}
void revive_响应(需要字符*resp、SSL*SSL、int body_){
字符头[1048576];
字符体[1048576];
int bytes;//实际读取的字节数
接收的int=0;//接收的字节数
int i,直线长度;
charc[1];
memset(头“\0”,sizeof(头));//头赋值=“\0”
memset(body'\0',sizeof(body));//body assign='\0'
/***********尝试逐字节读取***********/
i=0;
line_length=0;//检查每行的长度
做{
bytes=SSL_read(SSL,c,1);//将1字节读取到c[0]
if(字节可能被压缩,标题是否包含类似于Encoding:compress、gzip
或类似的内容?是否在请求中设置“接受编码”?谢谢。我在resquest中设置了Accept Encoding
)。我修正了它。不客气。你能在下面把你是如何修正它的吗?这可能会在将来帮助其他有同样问题的人。没什么:)。我只是删除了请求中的接受编码
。不过,直到有人(例如你)将此作为一个被接受的答案发布,这个问题才会显示为“未回答”。可能是压缩的,标题是否包含类似于编码:compress、gzip
或类似的内容?您是否在请求中设置了“接受编码”?谢谢。我在resquest:)中设置了接受编码。我修复了它。不客气。您可以在下面将如何修复它作为一个答案发布吗?这可能会帮助其他人我只是删除了请求中的Accept Encoding
。但是,直到有人(例如您)将此作为答案发布并被接受,此问题才会显示为“未回答”。