tcpclient - Web 客户端,用于获取 C 中的内容

tcpclient - web client to get content in c

下面是我必须从网站(例如 wiki)下载内容的代码

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
void error(char *msg)
{
    fprintf(stderr, "%s: %s\n", msg, strerror(errno));
    exit(1);
}

int main(int argc, char *argv[])
{
    int d_sock;
    char buf[255];
    char rec[256];
    int bytesRcvd;
    int c, result;
    struct addrinfo *res;
    struct addrinfo hints;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    // get ip from list of domains and puts it onto heap called naming resource
    getaddrinfo("en.wikipedia.org", "80", &hints, &res);

    /*Creating socket */
    d_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (d_sock == -1)
        error("Can't open socket");

    /*Connecting socket */
    c = connect(d_sock, res->ai_addr, res->ai_addrlen);
    freeaddrinfo(res);
    if (c == -1)
        error("Can't connect to socket");

    /* Sending GET */
    sprintf(buf, "GET /wiki/%s http/1.1\r\n", argv[1]);
    result = send(d_sock, buf, strlen(buf), 0);
    if (result == -1)
        fprintf(stderr, "%s: %s\n", "Error talking to the server",
    strerror(errno));

    /* Sending Host and blank line */
    memset(buf, 0, 255);
    strcpy(buf, "Host: en.wikipedia.org\r\n\r\n");
    result = send(d_sock, buf, strlen(buf), 0);
    if (result == -1)
        fprintf(stderr, "%s: %s\n", "Error talking to the server",
    strerror(errno));

    /*Receiving the page information */
    bytesRcvd = recv(d_sock, rec, 255, 0);
    while (bytesRcvd) {
        if (bytesRcvd == -1)
            error("Can't read from server");
        rec[bytesRcvd] = '[=10=]';
        printf("%s", rec);
        bytesRcvd = recv(d_sock, rec, 255, 0);
    }

    /*Closing socket */
    close(d_sock);
    return 0;
}

这里的问题是输出

HTTP/1.1 301 TLS Redirect
Server: Varnish
Location: https://en.wikipedia.org/wiki/apples
Content-Length: 0
Accept-Ranges: bytes
Date: Sat, 05 Dec 2015 06:46:13 GMT
...

发生了什么事,为什么要重定向?我的课程使用 head first c 书,我什至 运行 这个例子,它也被重定向,但他们的片段显示它成功地获得了内容

wikipedia 似乎现在将所有 http 请求重定向到 https,这就是为什么你得到这个 http 重定向,也许你应该考虑在另一个仍然使用 http 的网站上尝试你的代码(例如 Whosebug)