简体   繁体   English

如何接收HTTP响应

[英]How to receive HTTP Response

char url[]= "GET " PATH "\r\n"
"Host: " HOST "\r\n"
"User-Agent: Mozilla/5.0\r\n"
"\r\n";    
write(sfd, url, strlen(url)); // write(fd, char[]*, len);  
bzero(buffer, BUFFER_SIZE);
while(read(sfd, buffer, 1024 - 1) != 0){
    printf("%s", BUFFER_SIZE);
    bzero(buffer, 1024);
}

Result: 结果: 在此处输入图片说明

I'm writing a HTTP Client using Socket. 我正在使用Socket编写HTTP客户端。 The problem here is the read() func only return HTTP respond body. 这里的问题是read()函数仅返回HTTP响应主体。 But i want it to fetch http respond header also. 但我也希望它也获取http response标头。 I don't know what i'm doing wrong here. 我不知道我在做什么错。

The problem is that you are not specifying an HTTP version in the GET request, so the server will think you are a pre-1.0 client and won't send any headers to you. 问题是您没有在GET请求中指定HTTP版本,因此服务器将认为您是1.0之前的客户端,并且不会向您发送任何标头。

Do this instead: 改为这样做:

char url[]= "GET " PATH " HTTP/1.0\r\n" ...

Or: 要么:

char url[]= "GET " PATH " HTTP/1.1\r\n" ...

And then the server will give you headers. 然后服务器将为您提供标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C中使用OpenSSL接收http响应消息 - Receive http response messages using OpenSSL in C 如何使用zlib为HTTP压缩HTTP响应? - How to compress an HTTP response with zlib for HTTP? 如何在C套接字程序中接收HTTP GET和POST请求? - How to receive HTTP GET and POST requests in C socket program? C中的简单HTTP服务器 - 如何响应浏览器? - Simple HTTP Server in C - How to response to Browser? 如何在npapi插件中获取http响应标头 - How to get http response header in npapi plugin Django如何读取http请求并发送http响应 - how Django reads http request and sends http response 如何向libnl发送单通道扫描请求,以及如何接收对应通道的单通道扫描完成响应 - How to send single channel scan request to libnl, and receive single channel scan completion response for corresponding channel 在C语言中,如何使用libcurl将HTTP响应读取为字符串? - In C, how do you use libcurl to read a HTTP response into a string? 如何从 C 中的刺中提取特定字符(http 响应) - How to extract specific characters from a sting in C (http response) 如何从AVS通过http2连接接收超过65535字节的数据? - how to receive more than 65535 bytes of data on http2 connection from AVS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM