/************************************************************************************************************
Methods:
1. File()		post the specified file with specified name
2. FormField()	post form fields with specified name and value
3. Get()		perform GET method
4. Post()		perform POST method
5. URLField()	post form fields with specified name and value

Properties:
1. GetErrorReason() reports any errors that occur during the request
2. GetIsError()		returns 1 if any error occur during the request otherwise 0
3. PutPasswrod()	when required by a server, a valid password
4. PutPath()		the path to the directory in which a file is to be stored
5. PutPort()		the port number on the server from which the object is being requested. default port is 80
6. PutProtocol()	the Protocol property indicates what version of HTTP the request should be made using.d
					efault Protocol is HTTP/1.0
7. PutProxyPort()	the port number on the proxy server from which the object is being requested.
					default port is 80
8. PutProxyServer()	the proxy server address
9. PutReferrer()	address of a string that specifies the address (URL) of the document from which the URL
					in the request was obtained
10. PutSaveToFile()	the filename to be used for the file that is accessed
11. PutURL()		full URL of the host name or IP address of the server on which the file resides
12. PutUserAgent()	user agent request header
13. PutUserName()	when required by a server, a valid username
************************************************************************************************************/

/***********************************/
/*include all required header files*/
/***********************************/
#include <stdio.h>
#include <conio.h>

/*******************************************************************/
/*import AdvHTTP COM library here to get its methods and properties*/
/*******************************************************************/
#import "C:\AdvHTTP.dll" no_namespace

/*************************/
/*prototype for functions*/
/*************************/
void Get();//Get function
void Post();//Post function

/**************/
/*main program*/
/**************/
void main(void)
{
	char Ch;
	do
	{
		printf("\r\nGet	  [G]");
		printf("\r\nPost	  [P]");
		printf("\r\nQuit	  [Q]");
		printf("\r\n\r\nEnter Choice = ");
		scanf("%c", &Ch);
		getchar();
		Ch = toupper(Ch);
		switch (Ch)
		{
			case 'G':Get();break;//call Get function
			case 'P':Post();break;//call Post function
			case 'Q':printf("\r\nGood Bye\r\n");break;
			default: printf("\r\nError: Please enter valid choice\r\n");
		}
	}
	while (Ch != 'Q');
}

/*********************************************************************/
/*Get function: The function is used to perform Get operation of HTTP*/
/*********************************************************************/
void Get()
{
	char m_URL[256], m_ProxyServer[256], m_Path[256], m_SaveToFile[256];//define charecter buffer of 256 to read url, path to save file, file name and proxy server name if it is
	long l_ProxyPort;//define proxy port, if it is
	printf("\r\nEnter URL = ");
	gets(m_URL);//read url
	printf("\r\nEnter Proxy Server = ");
	gets(m_ProxyServer);//read proxy server
	printf("\r\nEnter Proxy Port = ");
	scanf("%d", &l_ProxyPort);
	getchar();
	printf("\r\nEnter Path = ");
	gets(m_Path);//read path where output file will be saved
	printf("\r\nEnter File Name = ");
	gets(m_SaveToFile);//read file name
	try//for COM library call, try...catch is used to handle exceptions
	{
		IHTTPPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IHTTPPtr("AdvHTTP.HTTP");
		obj->PutURL(m_URL);//set url property
		obj->PutProxyServer(m_ProxyServer);//set proxy server property
		obj->PutProxyPort(l_ProxyPort);//set proxy server port property
		obj->PutPath(m_Path);//set path property
		obj->PutSaveToFile(m_SaveToFile);//set file name property
		IDispatch *p = obj->Get();//call Get method of COM library
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
		{
			printf("\r\nSuccessfully Get\r\n");//display successfull message
			IGetPtr getobj;
			p->QueryInterface(IID_IDispatch,(void**)&getobj);//call query interface
			printf("\r\nMime Type = %s\r\nHeader = %s\r\nFile Content = %s\r\n",
				(char*)getobj->GetMimeType(), (char*)getobj->GetHeader(), (char*)getobj->GetFileContent());
		}
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/***********************************************************************/
/*Post function: The function is used to perform Post operation of HTTP*/
/***********************************************************************/
void Post()
{
	char m_URL[256], m_ProxyServer[256], m_Path[256], m_SaveToFile[256], m_FormFieldName[256], m_FormFieldValue[256];//define charecter buffer of 256 to read url, path to save file, for field name, value, file name and proxy server name if it is
	long l_ProxyPort;//define proxy port, if it is
	printf("\r\nEnter URL = ");
	gets(m_URL);//read url
	printf("\r\nEnter Proxy Server = ");
	gets(m_ProxyServer);//read proxy server
	printf("\r\nEnter Proxy Port = ");
	scanf("%d", &l_ProxyPort);
	getchar();
	printf("\r\nEnter Path = ");
	gets(m_Path);//read path where output file will be saved
	printf("\r\nEnter File Name = ");
	gets(m_SaveToFile);//read file name
	printf("\r\nEnter Form Field Name = ");
	gets(m_FormFieldName);//read form field name to be posted
	printf("\r\nEnter Form Field Value = ");
	gets(m_FormFieldValue);//read value for form field variable
	try//for COM library call, try...catch is used to handle exceptions
	{
		IHTTPPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = IHTTPPtr("AdvHTTP.HTTP");
		obj->PutURL(m_URL);//set url property
		obj->PutProxyServer(m_ProxyServer);//set proxy server property
		obj->PutProxyPort(l_ProxyPort);//set proxy server port property
		obj->PutPath(m_Path);//set path property
		obj->PutSaveToFile(m_SaveToFile);//set file name property
		obj->FormField(m_FormFieldName, m_FormFieldValue);//set form field method to post form variables
		// you can also write code here for posting URL variables and file also
		IDispatch *p = obj->Post();//call Post method of COM library
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
		{
			printf("\r\nSuccessfully Post\r\n");//display successfull message
			IPostPtr postobj;
			p->QueryInterface(IID_IDispatch,(void**)&postobj);//call query interface
			printf("\r\nMime Type = %s\r\nHeader = %s\r\nFile Content = %s\r\n",
				(char*)postobj->GetMimeType(), (char*)postobj->GetHeader(), (char*)postobj->GetFileContent());
		}
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}
