1

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
Serket
  • 3,785
  • 3
  • 14
  • 45
  • Can you show how you send the HTTP request? The domain name should be used in a header. – Joni Aug 07 '20 at 10:54
  • I don't understand this question. You get a 301 so it works at the basic http level. Therefore the TCP and IP layers below work well enough. Or are you missing that you use the URL at HTTP level? You don't send a hostname or an IP address to the web server – MSalters Aug 07 '20 at 10:55
  • An IP address can host more than one domain, so you have to send the name in the HTTP request. – stark Aug 07 '20 at 10:58
  • @Joni just added the request and response – Serket Aug 07 '20 at 10:59
  • @stark I did, look above – Serket Aug 07 '20 at 10:59
  • @MSalters I did, look above – Serket Aug 07 '20 at 10:59
  • That is not the "exact same URL" . See the S in HTTPS. You need a secure TLS connection for HTTPS – MSalters Aug 07 '20 at 11:01
  • @MSalters I added the time I tried sending using HTTPS, is it the problem? – Serket Aug 07 '20 at 11:02
  • The HTTP requests seem to be working correctly. The problem with the request you're sending with HTTPS seems to be that you're not using HTTPS: *"plain HTTP request was sent"* What code are you using to send requests - can you add that to the question? – Joni Aug 07 '20 at 11:09
  • @Joni I added the request I sent to the HTTPS port. I dont know what you mean by what code I'm using. The HTTPS request I sent is the last chunk of code. – Serket Aug 07 '20 at 11:12
  • You are sending the request with C function calls correct? What C function calls are you using to send the request? Which libraries are you using? – Joni Aug 07 '20 at 11:14
  • @Joni The libraries I'm using are mentioned in the question ( ). I am sending and receiving using the send and recv functions. – Serket Aug 07 '20 at 11:15
  • I see. That's not going to be enough for https. Do the answers here answer your question? https://stackoverflow.com/questions/16255323/make-an-https-request-using-sockets-on-linux – Joni Aug 07 '20 at 11:18
  • @Joni Ill try it right now. – Serket Aug 07 '20 at 11:19

0 Answers0