gpt4 book ai didi

c - 在 C 中使用套接字服务器的 HTML 头请求

转载 作者:可可西里 更新时间:2023-11-01 16:22:51 26 4
gpt4 key购买 nike

我正在尝试使用以下代码接收来自谷歌的回复。我正在寻找与您从 telnet www.google.com 收到的相同回复80 HEAD/HTTP/1.1 但它只是挂起。如果有人能在这里发现问题,我会非常感激

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
portno = 80;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
}

server = gethostbyname("www.google.com.au");
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;

bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);

if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
error("ERROR connecting");
}
puts("Retreiving HTML Head");

ssize_t nByte = write(sockfd,"HEAD / HTTP/1.1\r\n",strlen("HEAD / HTTP/1.1\r\n"));
if( nByte <= 0)
{
perror("send");
exit(EXIT_FAILURE);
}

size_t recived_len = 0;
if ((recived_len = read(sockfd,buffer,255)) == 0)
{
bzero(buffer,256);
printf("%s", buffer);
}

if (recived_len == -1)
{
perror("read");
}

close(sockfd);
return 0;
}

最佳答案

这是您的代码,稍作更改,从 read 返回 HTTP 响应。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg) {
perror(msg);
exit(0);
}

int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[256];
portno = 80;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("ERROR opening socket");
}

server = gethostbyname("www.google.com.au");
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;

bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);

if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("ERROR connecting");
}
puts("Retreiving HTML Head");

ssize_t nByte = write(sockfd, "HEAD / HTTP/1.1\r\n\r\n", strlen("HEAD / HTTP/1.1\r\n\r\n"));
if (nByte <= 0) {
perror("send");
exit(EXIT_FAILURE);
}

size_t recived_len = 0;
if ((recived_len = read(sockfd, buffer, 255)) > 0) {
//bzero(buffer, 256);
printf("%s", buffer);
}

if (recived_len == -1) {
perror("read");
}

close(sockfd);
return 0;
}

测试

Retreiving HTML Head
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Location: http://www.google.se/?gfe_rd=cr&ei=GZHSV-t69OTwB4uNp5gC
Content-Length: 256
Date: Fri, 09 Sep 2016 10:38:17 GMT

关于c - 在 C 中使用套接字服务器的 HTML 头请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39408353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com