I would like to download a PDF file with a socket. I receive a "400 Bad Request" error from the server, saying "The plain HTTP request was sent to HTTPS port", like shown in the image below (I open it as an HTML page):
I change the port from 443 to 80, but I don't get a response from the server. I have nothing in file.pdf
when I change the port to 80.
This is my console output:
Connected to server via socket 268 Bytes Sent: 109 Bytes received: 0 Received byte = 100 Total data = 100Bytes received: 0 Received byte = 100 Total data = 200Bytes received: 0 Received byte = 100 Total data = 300Bytes received: 0 Received byte = 100 Total data = 400Bytes received: 0 Received byte = 30 Total data = 430Connection closed Received byte = 0 Total data = 430Reply received
This is my code. I open the socket and I connect to the server, but I'm not receiving the file:
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup function failed with error: %d\n", iResult);
return 1;
}
sockaddr_in Serv_Addr;
//intialisation
struct addrinfo* result = NULL;
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
//socket configuration
SOCKET TcpClientSocket = -1;
int ret = 0;
TcpClientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (TcpClientSocket == INVALID_SOCKET) {
wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
Serv_Addr.sin_family = AF_INET;
Serv_Addr.sin_port = htons(443);
Serv_Addr.sin_addr.s_addr = inet_pton(AF_INET, "47.88.2.145", &Serv_Addr.sin_addr); //connect to www.axmag.com, link pdf file http://www.axmag.com/download/pdfurl-guide.pdf
printf("Connected to server via socket %u\n", TcpClientSocket);
//connection
iResult = -1;
iResult = connect(TcpClientSocket, (sockaddr*)& Serv_Addr, sizeof(Serv_Addr));
printf("%d", iResult);
if (iResult == SOCKET_ERROR) {
wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(TcpClientSocket);
if (iResult == SOCKET_ERROR)
wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// send request
const char* message;
message = "GET /download/pdfurl-guide.pdf HTTP/1.1\r\nHost: www.axmag.com\r\n\r\n Connection: keep-alive\r\n\r\n Keep-Alive: 300\r\n";
// test send succeded
iResult = send(TcpClientSocket, message, strlen(message), 0);
if (iResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(TcpClientSocket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
iResult = shutdown(TcpClientSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(TcpClientSocket);
WSACleanup();
return 1;
}
FILE* stream;
fopen_s(&stream, "1.html", "w+");
/////receive response from server
int totalData = 0, received_data = 0;
do
{
char server_reply[100];
received_data = recv(TcpClientSocket, server_reply, sizeof server_reply, 0);
if (received_data > 0)
printf("Bytes received: %d\n", iResult);
else if (received_data == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
totalData += received_data;
fwrite(server_reply, received_data, 1, stream);
printf("\nReceived byte = %d\nTotal data = %d", received_data, totalData);
} while (received_data > 0);
printf("Reply received\n");
if (stream)
{
if (fclose(stream))
{
printf("The file 'crt_fopen.c' was not closed\n");
}
}