Sign in to follow this  
musafir2007

Download file using code. help!

Recommended Posts

Ok, so I would like to use HTTP GET method to download a file from a web link. I have searched online and cannot find a simple code. Here's a bit of info: http://www.roseindia.net/servlets/GetAndPostMethodOfHttp.shtml All I want to do is get a file from a link and put it in a folder on my local drive and use c# to do this. Can someone provide or help me with simple sample code to do this? Btw, this is not HW, it's just interest and the reason I want to use HTTP GEt is because it's secure and doesn't require any additional tools. Thanks

Share this post


Link to post
Share on other sites
Heres my code i came up with but i cannot get it to write to file properly


#include <iostream>
#include <winsock.h>
#include <stdio.h>
#pragma comment( lib, "ws2_32.lib" )
using namespace std;

int main()
{
cout << "Http Sender 1.0" << endl;
cout << "Setting Up Winsock" << endl;
WSADATA wsaData;
int Result = WSAStartup( MAKEWORD( 1,1 ), &wsaData );
if(Result == 0)
{
cout << "Winsock Setup Success" << endl;
}
else
{
cout << "Winsock Setup Failed" << endl;
}

int RecvCount = 0;

Start:

SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if(Socket != INVALID_SOCKET)
{
cout << "Socket Setup Success" << endl;
}
else
{
cout << "Socket Setup Failed" << endl;
}

cout << "Enter Host Name" << endl;
char Hostname[64];
cin >> Hostname;

LPHOSTENT hostEntry;
hostEntry = gethostbyname(Hostname);//Get ip from Server by hostname

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

cout << "Enter Port Number" << endl;
int Port;
cin >> Port;
addr.sin_port = htons( Port ); //Set Port

if(connect( Socket, (LPSOCKADDR) &addr, sizeof(struct sockaddr) ) == SOCKET_ERROR)
{
cout << "Not Connected" << endl;
}
else
{
cout << "Connected" << endl;

cout << "Enter Packet To Send" << endl;

char SendBuffer[512];
char SendPacket[512];
char RecvPacket[5000];
int BytesRecv;
int TotalBytesRecv = 0;
DWORD RecvWritten;


//cin >> SendBuffer;
sprintf(SendPacket,"GET /notepad.exe HTTP/1.1\r\n"
"Host: 127.0.0.1\r\n"
"Connection: keep-alive\r\n\r\n");

int BytesSent = send(Socket,SendPacket,strlen(SendPacket),0);

cout << "BytesSent" << " " << BytesSent << endl;



//cout << RecvPacket << endl;
//cout << "BytesRecv" << " " << BytesRecv << endl;
HANDLE hFile =
CreateFileW(L"Notepad-downloaded.exe",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

while(1)
{
memset(&RecvPacket, 0, sizeof(RecvPacket));
BytesRecv = recv(Socket,RecvPacket,sizeof(RecvPacket),0);
cout << "BytesRecv" << " " << BytesRecv << endl;
RecvCount++; //keep count because the first packet has the header
cout << "RecvCount" << " " << RecvCount << endl;
if(BytesRecv > 0)
{
if(RecvCount == 1) //This is the packet we need to cut ,if i didnt do this other packets would be cut
{
cout << RecvPacket << endl;
cout << strlen(RecvPacket) << endl;
cout << 5000 - strlen(RecvPacket) << endl;


MessageBox(NULL,"Packet To Parse","",0);
char *CutHeaderOff = strstr(RecvPacket,"\r\n\r\n"); //Need to find start of Raw
if(!CutHeaderOff)
{
cout <<"Its NULL" << endl;
}
else
{
cout << CutHeaderOff << endl;
MessageBox(NULL,CutHeaderOff,"",0);
WriteFile(hFile,CutHeaderOff,sizeof(CutHeaderOff)+4,&RecvWritten,0);
}
}
else
{
WriteFile(hFile,RecvPacket,BytesRecv,&RecvWritten,0); //The other packets contain no headers
}

cout << "continuing" << endl;
continue;
}
else
{
cout << "TotalBytesRecv" << " " << TotalBytesRecv << endl;
cout << "Breaking" << endl;
closesocket(Socket);
break;
}

}


goto Start;

}

return 0;
}

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