Y
Y
Yozhik_v_tumane2018-05-24 23:45:46
C++ / C#
Yozhik_v_tumane, 2018-05-24 23:45:46

What is the correct way to send http request to winsock?

Hello! Help me to understand! I'm trying to send an http request to the site, but I don't get a response (nothing comes at all). I catch the result with a sniffer.

void myhttp() {
    std::string host = "cxem.net";
    int port = 80;
    std::string RQ = "GET / HTTP/1.1\r\n"
      "Host: " + host + "\r\n"
      "Connection: close\r\n\r\n";

    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
      *ptr = NULL,
      hints;
    int iResult;

    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) return;

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    iResult = getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &result);
    if (iResult != 0) {
      WSACleanup();
      return;
    }

    for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

      ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
        ptr->ai_protocol);
      if (ConnectSocket == INVALID_SOCKET) {
        WSACleanup();
        return;
      }

      iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
      if (iResult == SOCKET_ERROR) {
        closesocket(ConnectSocket);
        ConnectSocket = INVALID_SOCKET;
        continue;
      }
      break;
    }
    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
      WSACleanup();
      return;
    }

    iResult = send(ConnectSocket, RQ.c_str(), RQ.length(), 0);
    if (iResult == SOCKET_ERROR) {
      closesocket(ConnectSocket);
      WSACleanup();
      return;
    }

    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
      closesocket(ConnectSocket);
      WSACleanup();
      return;
    }
  }

Tried different requests, the effect is always the same

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur, 2018-05-25
Saifulmulukov @TiersWar

I would venture to guess that you didn't reverse endian to big endian for transmission over the network.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question