Hello everyone,
I'm trying to improve myself on Winsock , so I wrote a code which can get the content of an website.Now I'm trying to post (upload) a file to a http website.Here is my code , what should I add to my code to work with POST? Thanks for your helps.
#include <iostream> //Needed for input , output ops
#include <winsock2.h> //Needed for Connecting , HTTP , etc.
#include <windows.h> //Needed for Winsock
#include <string> //Needed for recv() , send()
#include <conio.h> //Needed for getch() command
#pragma comment(lib,"wsock32.lib") //Winsock library
using namespace std; //Easy way to use cout or cin without std::cout
int main()
{
WSADATA wsa;
SOCKET mysocket;
struct hostent *host;
char hostname[256]; //definitions
char directory[256];
char select[256];
char buffer[100000];
DWORD size;
char txtbuffer[100000];
int i = 0;
int s = 0;
string command;
SOCKADDR_IN SockAddr;
HANDLE hFile;
if(WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
cout << "Cannot start Winsock!" << endl; //Starting Winsock
cin.get();
}
mysocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); //Creating Socket
cout << "Enter host name = ";
cin >> hostname;
cout << endl;
cout << "----------------------------------" << endl; //Receiving host's name
cout << endl;
host = gethostbyname(hostname); //Finding host by name
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr); //Protocol definitions
connect(mysocket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)); //Connecting
cout << "Client :: --->> Connected!" << endl;
cout << "----------------------------------" << endl;
cout << endl;
cout << "Type : ";
cin >> select;
cout << endl;
cout << "Type ' / ' for scan or type directory of files you want to scan." << endl; //input for the directory to the search
cout << endl;
cin >> directory;
cout << endl;
cout << "----------------------------------" << endl;
cout << endl;
hFile = CreateFile("C:\\test.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
int dataRead = ReadFile(hFile,txtbuffer,sizeof(txtbuffer),&size,NULL);
cout << "Content of file : ";
while(dataRead > 0)
{
while(txtbuffer[s] > 0)
{
cout << txtbuffer[s]; //Printing it to the screen
s += 1;
}
break;
}
cout << endl;
cout << "Size of File : " << s << endl;
command += select;
command += " ";
command += directory;
command += " HTTP 1.1\r\nHost : ";
command += hostname;
command += "\r\n";
command += "Accept : */*\r\n";
command += "User-Agent : BNet\r\n";
command += "Content-Length : "What should I wrote here?"\r\n";
command += "Content-Type : text/html\r\n";
command += "\r\n\r\n";
cout << command.c_str();
cout << endl;
cout << endl;
cout << endl;
send(mysocket,command.c_str(),command.length(),0); //Request send
cout << "Client :: --->> Request Sent!" << endl;
cout << "----------------------------------" << endl;
cout << endl;
int dataLength = recv(mysocket,buffer,sizeof(buffer),0); //Receive any answers
while(dataLength != 0)
{
while(buffer[i] > 0)
{
cout << buffer[i]; //Printing it to the screen
i += 1;
}
break;
}
cout << endl;
cout << endl;
cout << "Bytes : " << i << endl; //Bytes of scanned file
cout << endl;
cout << endl;
closesocket(mysocket); //Cleanup
WSACleanup();
getch(); //loop trigger
//main();
}