我正在使用C / ++ <sys/socket.h> <netinet/in.h><arpa/inet.h>
中的POSIX套接字发出原始HTTP请求。我尝试使用网站的IP地址来获取请求,但它返回的状态码为301,网址完全相同,但使用域名而不是IP地址。如何在C POSIX套接字(如Python套接字)中按域名访问服务器?我认为问题可能是我使用HTTP而不是HTTPS,但是我尝试使用HTTPS却无法正常工作(请参见下面的请求和响应)
我想做这样的事情:
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server;
server.sin_port = htons(80);
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("www.google.com"); //This is the problematic line (and
//google isn't the website I want
//to access, this is just a
//placeholder.)
connect(s, (struct sockaddr*)&server, sizeof(server));
这是我发送的HTTP请求:
GET /v1/last/stocks/AAPL?APCA-API-KEY-ID=My_Token&APCA-API-SECRET-KEY=My_Secret_Key& HTTP/1.1
Host: data.alpaca.markets
我已将其发送到此服务器:35.186.171.205/v1/last/stocks/AAPL
这是我得到的答复:
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.1
Date: Fri, 07 Aug 2020 10:38:31 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://data.alpaca.markets/v1/last/stocks/AAPL?APCA-API-KEY-ID=My_Token&APCA-API-SECRET-KEY=My_Secret_Key&
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
我尝试将其发送到HTTPS端口(443),但是得到了:
HTTP/1.1 400 Bad Request
Server: nginx/1.16.1
Date: Fri, 07 Aug 2020 10:38:06 GMT
Content-Type: text/html
Content-Length: 255
Connection: close
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
这是我使用HTTPS而不是HTTP发送的请求(相同,是否有问题?)
GET /v1/last/stocks/AAPL??APCA-API-KEY-ID=My_Token&APCA-API-SECRET-KEY=My_Secret_Key& HTTP/1.1
Host: data.alpaca.markets