I am making raw HTTP requests using POSIX sockets in C/++ <sys/socket.h> <netinet/in.h><arpa/inet.h>
. I've tried using the IP address of the website to get a request, but it returns a status code of 301 and the exact same URL but with the domain name instead of the IP address. How can I access servers by their domain name in C POSIX sockets (like in Python sockets)? I think the problem might be that I use HTTP instead of HTTPS, but I've tried using HTTPS and it didn't work (look below at the request and response)
I want to do something like this:
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));
Here is the HTTP request I'm sending:
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
I sent it to this server: 35.186.171.205/v1/last/stocks/AAPL
This is the response I'm getting:
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>
I've tried sending it to the HTTPS port (443) but got this:
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>
This is the request I sent using HTTPS instead of HTTP (its the same, is that a problem?):
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