Не отправляется запрос через сокеты(выдает ошибку 400 bad request)
Code:
#include <iostream>
#include <WinSock2.h>
#include <string>
#define PORT 80
#pragma warning(disable: 4996)
#pragma comment(lib, "ws2_32.lib")
using namespace std;
const char* link = "api.telegram.org";
int main()
{
setlocale(LC_ALL, "rus");
WSADATA wsaData;
WORD Dllver = MAKEWORD(2, 1);
string post = "POST /botTOKEN/getMe HTTP/1.1\r\nHost: api.telegram.org\r\n";
post += "Content-Type: application/x-www-form-urlencoded\r\n";
post += "Content-Length: " + to_string(post.length()) + "\r\n";
post += "Connection: close\r\n\r\n";
char msg[4096];
memset(msg, 0, 4096);
if (WSAStartup(Dllver, &wsaData) == 0)
{
SOCKADDR_IN addr;
addr.sin_addr.s_addr = *((unsigned long*)gethostbyname(link)->h_addr);
addr.sin_port = htons(PORT);
addr.sin_family = PF_INET;
SOCKET connection = socket(PF_INET, SOCK_STREAM, NULL);
if (connection != NULL)
{
if (connect(connection, (SOCKADDR*)&addr, sizeof(addr)) == 0)
{
cout << "All good!" << endl;
if (send(connection, post.c_str(), post.size(), NULL) == SOCKET_ERROR)
{
cout << "Send error" << endl;
}
recv(connection, msg, sizeof(msg), NULL);
cout << msg << endl;
}
else
{
cout << "Connection error";
exit(EXIT_FAILURE);
}
}
closesocket(connection);
WSACleanup();
}
else
exit(EXIT_FAILURE);
return EXIT_SUCCESS;
}