WinSock http GET запрос
Я сделал http GET запрос на сокетах, но столкнулся с проблемой: как можно заранее узнать длину ответа от сервера и нужно ли это вообще? вот код запроса (ответа):
bool SendHttpGETRequest(LPCSTR PTFWP, LPCSTR site)
{
string get = "GET "s + PTFWP + " HTTP/1.1\r\nHost: " + site + "\r\nConnection: close\r\n\r\n";
if (send(servSock, get.c_str(), get.size(), 0) <= 0) {
result = "Error\0";
return false;
}
char* resHeaders = new char[310];
if (recv(servSock, resHeaders, 310, 0) < 0) {
result = "Error!\0";
return false;
}
result = resHeaders;
result = result.substr(result.find("Content-Length:") + 15);
result = result.substr(0, result.find_first_of("\r\n"));
int bufSize = stoi(result);
delete resHeaders;
resHeaders = new char[bufSize + 1];
int readOfBytes = 0;
int AllSize = 0;
result = "";
while (((readOfBytes = recv(servSock, resHeaders, bufSize, 0)) > 0) && ((AllSize+=readOfBytes)<= bufSize)) {
result += resHeaders;
}
result[bufSize] = '\0';
if ((readOfBytes < 0) && result.empty())
return false;
return true;
}
Прошу сильно не ругать, я новичок в этой теме!
Не могу написать в виде ответа (туповаты правила на этом сайте) поэтому напишу здесь! Следующий код работает корректно (я нашёл способ принимать ответ от сервера):
bool CheckHTTPHeaders(string headers) {
if (headers.empty())
return false;
string oneHead = headers.substr(0, headers.find_first_of("\r\n"));
headers = headers.substr(oneHead.size()+1);
if (oneHead != "HTTP/1.1 200 OK")
return false;
try
{
headers = headers.substr(headers.find("Content-Length:")+15);
oneHead = headers.substr(0, headers.find("\r\n"));
contentLength = stoi(oneHead);
return true;
}
catch (...)
{
return false;
}
}
bool SendHttpGETRequest(LPCSTR PTFWP, LPCSTR site)
{
string get = "GET "s + PTFWP + " HTTP/1.1\r\nHost: " + site + "\r\nConnection: close\r\n\r\n";
if (send(servSock, get.c_str(), get.size(), 0) <= 0) {
result = "Error\0";
return false;
}
char* res = new char[1024];
int AllReadBytes = 0;
if ((AllReadBytes = recv(servSock, res, 1024, 0)) < 0) {
result = "Internet error\0";
return false;
}
result = res;
string headers = result.substr(0, result.find("\r\n\r\n"));
result = result.substr(result.find("\r\n\r\n"));
if (!CheckHTTPHeaders(headers)) {
result = "SERVER SEND ERROR HEADER!\0";
return false;
}
if (AllReadBytes < contentLength) {
delete res;
res = new char[contentLength];
int ReadBytes;
if ((ReadBytes = recv(servSock, res, contentLength, 0)) < 0) {
result = "ERROR CONTENT!\0";
return false;
}
AllReadBytes += ReadBytes;
if (AllReadBytes < contentLength) {
return false;
}
res[ReadBytes] = '\0';
result += res;
delete res;
}
return true;
}
Ответы (1 шт):
Автор решения: Kromster
→ Ссылка
Перенёс ответ автора из тела вопроса
Следующий код работает корректно (я нашёл способ принимать ответ от сервера):
bool CheckHTTPHeaders(string headers) {
if (headers.empty())
return false;
string oneHead = headers.substr(0, headers.find_first_of("\r\n"));
headers = headers.substr(oneHead.size()+1);
if (oneHead != "HTTP/1.1 200 OK")
return false;
try
{
headers = headers.substr(headers.find("Content-Length:")+15);
oneHead = headers.substr(0, headers.find("\r\n"));
contentLength = stoi(oneHead);
return true;
}
catch (...)
{
return false;
}
}
bool SendHttpGETRequest(LPCSTR PTFWP, LPCSTR site)
{
string get = "GET "s + PTFWP + " HTTP/1.1\r\nHost: " + site + "\r\nConnection: close\r\n\r\n";
if (send(servSock, get.c_str(), get.size(), 0) <= 0) {
result = "Error\0";
return false;
}
char* res = new char[1024];
int AllReadBytes = 0;
if ((AllReadBytes = recv(servSock, res, 1024, 0)) < 0) {
result = "Internet error\0";
return false;
}
result = res;
string headers = result.substr(0, result.find("\r\n\r\n"));
result = result.substr(result.find("\r\n\r\n"));
if (!CheckHTTPHeaders(headers)) {
result = "SERVER SEND ERROR HEADER!\0";
return false;
}
if (AllReadBytes < contentLength) {
delete res;
res = new char[contentLength];
int ReadBytes;
if ((ReadBytes = recv(servSock, res, contentLength, 0)) < 0) {
result = "ERROR CONTENT!\0";
return false;
}
AllReadBytes += ReadBytes;
if (AllReadBytes < contentLength) {
return false;
}
res[ReadBytes] = '\0';
result += res;
delete res;
}
return true;
}