I'm new to socket programming and there is this site which provides dummy api users data. I'm programming to get it from win32. This is basically the code from here. I just removed the error checks for now, just to make it more readable.
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo* result = NULL,
* ptr = NULL,
hints;
const char* sendbuf = "GET /api/users HTTP/1.1\r\nHost: reqres.in\r\n\r\n";
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_SECURE;
// Resolve the server address and port
iResult = getaddrinfo("reqres.in", "443", &hints, &result);
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
break;
}
freeaddrinfo(result);
// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
} while (iResult > 0);
// cleanup
closesocket(ConnectSocket);
WSACleanup();
this results in a "HTTP/1.1 400 Bad Request" after recv function. Which I dont get it. In curl a similar thing would be curl -v https://reqres.in/api/users:443
and this works! However curl -v reqres.in/api/users:443
doesn't. Can someone help me understant all this? Thanks
Edit: I forget additional info about error: "the plain HTTP request was sent to HTTPS port"