Я нашел здесь пример, но продолжаю получать следующую ошибку. Я пробовал различные комбинации в заголовке, но безрезультатно ... Запросы почтальона работают нормально. Что-то не хватает ... Это порт 443 ... Нужно ли использовать другой метод? Как вы узнаете из прочтения, я не знаком с запросами HTTPS, но мне нужно добавить это в свое программное обеспечение, чтобы получать последние данные с сервера. Приветствуется любая помощь ...
HTTP / 1.1 400 Сервер неверных запросов: awselb / 2.0 Дата: Пн, 25 мая 2020 г. 16:52:31 GMT Content-Type: text / html Content-Length: 236 Подключение: закрыть
#include <windows.h>
#include <string>
#include <stdio.h>
#include <winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
using std::string;
...
// Initialize Dependencies to the Windows Socket.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
fprintf(fpSessionLog,"WSAStartup failed.\n");
fflush(fpSessionLog);
goto SKIP;
}
// We first prepare some "hints" for the "getaddrinfo" function
// to tell it, that we are looking for a IPv4 TCP Connection.
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET; // We are targeting IPv4
hints.ai_protocol = IPPROTO_TCP; // We are targeting TCP
hints.ai_socktype = SOCK_STREAM; // We are targeting TCP so its SOCK_STREAM
// Aquiring of the IPv4 address of a host using the newer
// "getaddrinfo" function which outdated "gethostbyname".
// It will search for IPv4 addresses using the TCP-Protocol.
struct addrinfo* targetAdressInfo = NULL;
DWORD getAddrRes = getaddrinfo("api-thisSite.com", NULL, &hints, &targetAdressInfo);
if (getAddrRes != 0 || targetAdressInfo == NULL)
{
fprintf(fpSessionLog,"Could not resolve the Host Name\n");
fflush(fpSessionLog);
WSACleanup();
goto SKIP;
}
// Create the Socket Address Informations, using IPv4
// We dont have to take care of sin_zero, it is only used to extend the length of SOCKADDR_IN to the size of SOCKADDR
SOCKADDR_IN sockAddr;
sockAddr.sin_addr = ((struct sockaddr_in*) targetAdressInfo->ai_addr)->sin_addr; // The IPv4 Address from the Address Resolution Result
sockAddr.sin_family = AF_INET; // IPv4
sockAddr.sin_port = htons(443);
// We have to free the Address-Information from getaddrinfo again
freeaddrinfo(targetAdressInfo);
// Creation of a socket for the communication with the Web Server,
// using IPv4 and the TCP-Protocol
SOCKET webSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (webSocket == INVALID_SOCKET)
{
fprintf(fpSessionLog,"Creation of the Socket Failed\n");
fflush(fpSessionLog);
WSACleanup();
goto SKIP;
}
// Establishing a connection to the web Socket
//cout << "Connecting...\n";
fprintf(fpSessionLog,"Connecting...\n");
fflush(fpSessionLog);
if(connect(webSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) != 0)
{
fprintf(fpSessionLog,"Could not connect!\n");
fflush(fpSessionLog);
closesocket(webSocket);
WSACleanup();
goto SKIP;
}
fprintf(fpSessionLog,"Connected!\n");
fflush(fpSessionLog);
// Sending a HTTP-GET-Request to the Web Server
const char* httpRequest = "GET /api/v1/ecm HTTP/1.1\r\nHost: api-thisSite.com\r\nX-Authorization: {api key goes here}\r\nAccept: application/json"
fprintf(fpSessionLog,"Formulated Server Request: %s \n",httpRequest);
fflush(fpSessionLog);
int sentBytes = send(webSocket, httpRequest, strlen(httpRequest),0);
if (sentBytes < strlen(httpRequest) || sentBytes == SOCKET_ERROR)
{
//cout << "Could not send the request to the Server" << endl;
//system("pause");
fprintf(fpSessionLog,"Could not send the request to the Server!\n");
fflush(fpSessionLog);
closesocket(webSocket);
WSACleanup();
goto SKIP;
}
// Receiving and Displaying an answer from the Web Server
char buffer[10000];
ZeroMemory(buffer, sizeof(buffer));
int dataLen;
while ((dataLen = recv(webSocket, buffer, sizeof(buffer), 0) > 0))
{
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
//cout << buffer[i];
fprintf(fpSessionLog,"%c",buffer[i]);
i += 1;
}
}
fflush(fpSessionLog);
// Cleaning up Windows Socket Dependencies
closesocket(webSocket);
WSACleanup();
SKIP:
HTTP/1.1 400 Bad Request
Server: awselb/2.0
Date: Mon, 25 May 2020 20:32:40 GMT
Content-Type: text/html
Content-Length: 236
Connection: close
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
</body>
</html>
Как отправить HTTPS-запрос на HTTPS-порт?