【问题标题】:Getting html page using get http request and sockets in C使用 C 中的 get http 请求和套接字获取 html 页面
【发布时间】:2021-07-09 18:14:06
【问题描述】:

我正在尝试打印 html 页面 https://pastebin.com/raw/7y7MWssc

这是我的代码:

#include <stdio.h>
#include <winsock2.h>

void main(){
    WSADATA WSA;
    WSAStartup(MAKEWORD(2,2),&WSA) ;

    char  Request[] = "GET /raw/7y7MWssc HTTP/1.1\r\n\r\n" ;
    char  Response[2000] ;

    SOCKET  Socket = socket(AF_INET , SOCK_STREAM , 0 );
    struct sockaddr_in Server;
    struct hostent *H = gethostbyname("pastebin.com") ;

    Server.sin_addr.s_addr  = *( (int *)H->h_addr);
    Server.sin_family       = AF_INET;
    Server.sin_port         = htons( 80 );

    connect(Socket , (struct sockaddr *)&Server , sizeof(Server)) ;
    send(Socket , Request , strlen(Request) , 0)  ;
    int Rs = recv(Socket , Response , 2000 , 0) ;
    Response[Rs] = 0 ;
    printf("%s\n", Response );

    closesocket(Socket);
    WSACleanup();
}

但我不断收到 400 Bad Request 作为响应,但是当请求是“GET /raw/7y7MWssc HTTP/1.1\r\nHost: pastebin.com\r\n\r\n”时,我得到 301 Moved Permanently to地点:https://pastebin.com/raw/7y7MWssc 感谢您的帮助

【问题讨论】:

  • 听起来你知道如何解决它?
  • 它永久移动到 https 而不是 http,这意味着服务器不再接受常规 HTTP,而只接受基于 TLS 的安全 HTTP。

标签: c sockets http


【解决方案1】:

您收到400 Bad Request 错误,因为您发送的请求无效。您已声明您正在使用HTTP/1.1,但HTTP/1.1 请求必须包含Host: 标头。比较一下:

$ telnet pastebin.com 80
Trying 104.23.98.190...
Connected to pastebin.com.
Escape character is '^]'.
GET /raw/7y7MWssc HTTP/1.1

HTTP/1.1 400 Bad Request
Server: cloudflare
Date: Wed, 14 Apr 2021 17:48:12 GMT
Content-Type: text/html
Content-Length: 155
Connection: close
CF-RAY: -

到这里:

$ telnet pastebin.com 80
Trying 104.23.98.190...
Connected to pastebin.com.
Escape character is '^]'.
GET /raw/7y7MWssc HTTP/1.1
Host: pastebin.com

HTTP/1.1 301 Moved Permanently
Date: Wed, 14 Apr 2021 17:49:32 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Wed, 14 Apr 2021 18:49:32 GMT
Location: https://pastebin.com/raw/7y7MWssc
cf-request-id: 097319bbec00005b119e0df000000001
Server: cloudflare
CF-RAY: 63fec562aa1c5b11-IAD

请注意第二个请求(包含 Host: 标头)如何返回预期响应而不是 400 Bad Request 错误。


要真正从最终位置获取内容,您需要更新代码以发出https 请求。

【讨论】:

  • 知道如何发出 https 请求吗?
  • 这里的 cmets 并不是真正适合讨论的地方。我认为这里对 SO 的一些简短搜索可能会导致一些相关的示例代码。
  • 为此使用 HTTP 库。您可能会遇到不想处理的编码和压缩(如:自己实现)。
  • @SinusPi HTTPS 只是通过 SSL/TLS 加密连接的 HTTP。这意味着您必须与端口 443 建立新的 TCP 连接并协商 SSL/TLS 握手,然后才能通过该现在加密的连接发送任何 HTTP 请求。使用库为您处理加密,例如 OpenSSL 等。不要尝试自己实现 SSL/TLS。
猜你喜欢
  • 2017-12-31
  • 2015-12-29
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多