0

well so i've made a simple wrapper class to facilitate uploading a text file to my server.....and i can't seem to find the problem.....would anyone be kind enough to see where i did wrong?

HTTPPost wrapper class:

#ifndef _HTTP_POSTER_
#define _HTTP_POSTER_

#include <windows.h>
#include <wininet.h>
#include <tchar.h>
#include <fstream>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;

#pragma comment(lib,"wininet.lib")

#define HTTP_POST "POST"
#define HTTP_GET "GET"

typedef struct {
	string mainHeader;
	string name;
	string fileName;
	string contentType;
	string fileContents;
} PostFileHeader;


class HTTPPoster {
	HINTERNET _hSession;
	HINTERNET _hConnect;
	HINTERNET _hRequest;
	
	char* _hostName;
	char* _agent;
	
	public:
		HTTPPoster();
		~HTTPPoster();
		
		bool init(char* hostName = "", char* agent = "");
		bool openRequest(char* reqType, char* reqPath, char** acceptedTypes);
		BOOL send(PostFileHeader &header);
		
		static char **getAllowedTypes();
		static const string BOUNDARY;
};


void newHeader(PostFileHeader &newHeader, string name, string fileName, string contentType = "text/plain");
string getMainHeader(const PostFileHeader &header);
string getAddHeaders(const PostFileHeader &header);
string getFileContents(char* fileName, PostFileHeader &header);


#endif

HTTPPost.cpp:

#include "HTTPPoster.h"


HTTPPoster::HTTPPoster() {
}

HTTPPoster::~HTTPPoster() {
	if (_hSession) InternetCloseHandle(_hSession);
    if (_hConnect) InternetCloseHandle(_hConnect);
    if (_hRequest) InternetCloseHandle(_hRequest);
}

bool HTTPPoster::init(char* hostName, char* agent) {
	_hostName = hostName;
	_agent = agent;
	
	_hSession = InternetOpen(_agent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
	if (_hSession == NULL) {
		return false;
	}
	
	_hConnect = InternetConnect(_hSession, _T(_hostName), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
	if (_hConnect == NULL) {
		return false;
	}
	
	return true;
}

bool HTTPPoster::openRequest(char* reqType, char* reqPath, char** acceptedTypes) {
	_hRequest = HttpOpenRequest(_hConnect, reqType, _T(reqPath), NULL, NULL, (const char**)acceptedTypes, 0, 1);
	if (_hRequest == NULL) {
		return false;
	}
	
	return true;
}

BOOL HTTPPoster::send(PostFileHeader &header) {
	char* mainHeader = const_cast<char*>(getMainHeader(header).c_str());
	char* addHeaders = const_cast<char*>(getAddHeaders(header).c_str());
	if (_hRequest == NULL) {cout << "asdasd" << endl; }
	BOOL sent = HttpSendRequest(_hRequest, mainHeader, strlen(mainHeader), addHeaders, strlen(addHeaders));
	return sent;
}

const string HTTPPoster::BOUNDARY = "-------------------aadf0a9cb2a";


void newHeader(PostFileHeader &newHeader, string name, string fileName, string contentType) {
	newHeader.mainHeader = "Content-Type: multipart/form-data; boundary=" + HTTPPoster::BOUNDARY;
	newHeader.name = name;
	newHeader.fileName = fileName;
	newHeader.contentType = contentType;
	newHeader.fileContents = "";
}

string getMainHeader(const PostFileHeader &header) {
	return header.mainHeader;
}

string getAddHeaders(const PostFileHeader &header) {
	string addHeaders = "";
	addHeaders += HTTPPoster::BOUNDARY + "\r\n";
	addHeaders += "Content-Disposition: form-data; name=\"" + header.name + "\"; ";
	addHeaders += "filename=\"" + header.fileName + "\"\r\n";
	addHeaders += "Content-Type: " + header.contentType + "\r\n\r\n";
	addHeaders += header.fileContents + "\r\n\r\n" + HTTPPoster::BOUNDARY + "--\r\n";
	
	return addHeaders;
}

string getFileContents(char* fileName, PostFileHeader &header) {
	ifstream file(fileName);
	if (!file.is_open()) {
		return "\0";
	}
	
	string contents = "", buffer = "";
	while (getline(file, buffer, '\n')) {
		contents += buffer + "\n";
	}
	
	header.fileContents = contents;
	file.close();
	
	return contents;
}

char** HTTPPoster::getAllowedTypes() {
	return (char**)"*/*\0";
}

and finally the implementation file:

#include "HTTPPoster.h"


int main() {
	HTTPPoster poster;
	
	bool suc;
	
	suc = poster.init("myserver.com", "MyAgent");
	if (!suc) {
		cout << "Error init()" << endl;
		exit(1);
	}
	
	PostFileHeader fHeader;
	newHeader(fHeader, "file", "test.txt");
	getFileContents("test.txt", fHeader);
	
	cout << getMainHeader(fHeader) << endl;
	cout << getAddHeaders(fHeader) << endl;
	
	poster.openRequest(HTTP_POST, "upload.php", HTTPPoster::getAllowedTypes());
	if (!suc) {
		cout << "Error openReq()" << endl;
		exit(1);
	}
	
	suc = poster.send(fHeader);
	if (suc == FALSE) {
		cout << "Error send()" << endl;
		exit(1);
	}
	
	return 0;
}

and here's the receiving php code which i've tested with a html form and it works:

<?php
	if ($_FILES['file']['size'] > 0) {
		$filename = "uploadedFile.txt";
		move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $filename);
		echo "File saved at " . "upload/" . $filename . "<br>";
	} else {
		echo "Invalid File.";
	}
?>

Everything compiles fine and no errors show up when ran....but the file isn't found on my server as well. I think maybe it is my HTTP Request header that is wrong?

Edited by dzruptz: n/a

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by dzruptz
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.