Sign in to follow this  
Aidamina

File Downloading through HTTP

Recommended Posts

Well heres the deal the last couple of days i've been f*cking arround with WinHTTP and WinInet. But its giving me all sort of weird errors and i am wondering if any1 has some suggestions and or examples. Whats the best way to get files through HTTP, I could use some examples. Greetz, Me

Share this post


Link to post
Share on other sites
Well, the best way to do it, as I see it, is to make a class that parses the raw data from a socket-connection. It's not that hard to do actually - you just have to read the RFCs carefully...

For reference have a look at the HTML 1.1 RFC at http://rfc.sunsite.dk/rfc/rfc2616.html - and if any other RFCs are needed, links will be there, or at least RFC-numbers which are easily looked up on http://rfc.sunsite.dk/ or sites like that.

Share this post


Link to post
Share on other sites
I use the raw socket method extensively. It's easy peasy. All you need to do is connect to the server (e.g. gamedev.net), and send this:

"GET "+strPage+" HTTP/1.1\r\n"
"Host: "+strHost+"\r\n"
"User-Agent: My HTTP file downloader thingy\r\n"
"Connection: close\r\n"
"\r\n";


Where strHost is the address of the site you're connecting to (e.g. "gamedev.net"), and strPage is the page you're requesting, prefixed with a / (e.g. "/" or "/file.jpg").
Then you just read from the socket until the connection gets closed by the server, and you can skip the HTTP header (which ends with "\r\n\r\n").

Share this post


Link to post
Share on other sites
Well i got this and it seems to work,
BUT i dont get any responds from the server.
Any suggestions?



#include <stdio.h>
#include "winsock2.h"
#include <iostream>

void main() {

// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");

// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}

// Connect to a server.
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "70.84.85.186" );
clientService.sin_port = htons( 80 );

if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}

// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char strHost[]="www.eoeforums.com";
char strPage[]="index.php";

char query[512];
sprintf(query, "GET %s HTTP/1.1\r\\nHost: %s \\r\\nUser-Agent: My HTTP file downloader thingy\\r\\nConnection: close\\r\\n\\r\\n", strHost, strPage);

char sendbuf[512] = "Client: Sending data.";
char recvbuf[256*256] = "";
sprintf(sendbuf, query);

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );

while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 256*256, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return;
printf( "Bytes Recv: %ld\n", bytesRecv );
}

char temp;
std::cin >>temp;

return;
}

Share this post


Link to post
Share on other sites
The problem is that You need to send the escape code "\r\n" for the end of a line, not the literal string "\\r\\n". So the server thinks that's one line, and is waiting for the "\r\n" signifying end of request.

Any you can send it in as many packets as you like. You can even use telnet (1 packet per charavter usually) if you want.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this