0

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"

  • 1
    You are accessing port 443 (the default port for HTTPS) but aren't using a socket capable of doing the SSL. I don't know how to do this using Windows Sockets, so I'll have to direct you to [Secure Winsock Programming](https://learn.microsoft.com/en-us/windows/win32/winsock/secure-winsock-programming). – IInspectable Aug 24 '23 at 14:29
  • Thats interesting, I tried changing the port to 80 in the code, and I just receive garbage. So, would it correct to assume that this website doesn't have handle the http port? – Marco Vinicio Aug 24 '23 at 14:42
  • 1
    Sending `curl -v http://reqres.in/api/users` (or `curl -v http://reqres.in:80/api/users`) produces reasonable output for me (a 301). Take note that the port you're specifying is in the wrong location. It needs to follow the hostname. – IInspectable Aug 24 '23 at 14:52
  • As an aside, if you want curl to follow redirects, you can pass `-L` or `--location` along. Executing `curl -v -L http://reqres.in:80/api/users` produces the expected JSON response. – IInspectable Aug 24 '23 at 19:46
  • 1
    Two more notes: `1` If you look at the initial 301 response for the HTTP request, you'll see that it redirects to the HTTPS resource, so you'll have to implement SSL regardless. `2` Sockets only provide network access. Implementing the HTTP protocol is on you. Specifically, if you receive a 301 response, you'll have to extract the `Location:` header, parse the destination URI, verify that it uses a scheme you support (e.g. `https`), connect a new socket, and send another request. All of that can wait until you have a working SSL implementation. – IInspectable Aug 24 '23 at 20:43
  • @MarcoVinicio Is this helpful? https://stackoverflow.com/questions/74919755/win32-multithreaded-sockets/74926565#74926565 You have to note `sin_family ` is wrong. It should be ipv4(AF_INET) under that circumstance. You can modify `sin_family `and `sin_port ` based on your requirements. – Torrecto - MSFT Aug 25 '23 at 02:46
  • There is no point in using a `for` loop on the result of `getaddrinfo()` if you are just going to `break` unconditionally after the 1st iteration. You should be looping through the entire list until `connect()` is successful, as `getaddrinfo()` may have returned multiple IPs, and not all of them may be reachable from your location – Remy Lebeau Aug 25 '23 at 18:01
  • If you don't want to implement the SSL/TLS portion yourself (which is not trivial to implement by hand), then consider using the WinInet or WinHTTP library instead. Or even libcurl – Remy Lebeau Aug 25 '23 at 18:03

0 Answers0