home *** CD-ROM | disk | FTP | other *** search
/ Internet Pratica / IPRAT_01.iso / ASP / ASPFusion_Basic_Suite / examples / c++ / advhttp / AdvHTTP.cpp next >
Encoding:
C/C++ Source or Header  |  2002-01-26  |  7.5 KB  |  173 lines

  1. /************************************************************************************************************
  2. Methods:
  3. 1. File()        post the file specified in path parameter with name specified in name parameter
  4. 2. FormField()    post form fields with specified name and value
  5. 3. Get()        perform GET method
  6. 4. Post()        perform POST method
  7. 5. URLField()    post form fields with specified name and value
  8.  
  9. Properties:
  10. 1. GetErrorReason() reports any errors that occur during the request
  11. 2. GetIsError()        returns 1 if any error occur during the request otherwise 0
  12. 3. PutPasswrod()    when required by a server, a valid password
  13. 4. PutPath()        the path to the directory in which a file is to be stored
  14. 5. PutPort()        the port number on the server from which the object is being requested. default port is 80
  15. 6. PutProtocol()    the Protocol property indicates what version of HTTP the request should be made using.d
  16.                     efault Protocol is HTTP/1.0
  17. 7. PutProxyPort()    the port number on the proxy server from which the object is being requested.
  18.                     default port is 80
  19. 8. PutProxyServer()    the proxy server address
  20. 9. PutReferrer()    address of a string that specifies the address (URL) of the document from which the URL
  21.                     in the request was obtained
  22. 10. PutSaveToFile()    the filename to be used for the file that is accessed
  23. 11. PutURL()        full URL of the host name or IP address of the server on which the file resides
  24. 12. PutUserAgent()    user agent request header
  25. 12. PutUserName()    when required by a server, a valid username
  26. ************************************************************************************************************/
  27.  
  28. /***********************************/
  29. /*include all required header files*/
  30. /***********************************/
  31. #include <stdio.h>
  32. #include <conio.h>
  33.  
  34. /************************************************************************/
  35. /*import AdvDirectory COM library here to get its methods and properties*/
  36. /************************************************************************/
  37. #import "C:\AdvHTTP.dll" no_namespace
  38.  
  39. /*************************/
  40. /*prototype for functions*/
  41. /*************************/
  42. void Get();//Get function
  43. void Post();//Post function
  44.  
  45. /**************/
  46. /*main program*/
  47. /**************/
  48. void main(void)
  49. {
  50.     char Ch;
  51.     do
  52.     {
  53.         printf("\r\nGet    [G]");
  54.         printf("\r\nPost    [P]");
  55.         printf("\r\nQuit    [Q]");
  56.         printf("\r\n\r\nEnter Choice = ");
  57.         scanf("%c", &Ch);
  58.         getchar();
  59.         Ch = toupper(Ch);
  60.         switch (Ch)
  61.         {
  62.             case 'G':Get();break;//call Get function
  63.             case 'P':Post();break;//call Post function
  64.             case 'Q':printf("\r\nGood Bye\r\n");break;
  65.             default: printf("\r\nError: Please enter valid choice\r\n");
  66.         }
  67.     }
  68.     while (Ch != 'Q');
  69. }
  70.  
  71. /*********************************************************************/
  72. /*Get function: The function is used to perform Get operation of HTTP*/
  73. /*********************************************************************/
  74. void Get()
  75. {
  76.     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
  77.     long l_ProxyPort;//define proxy port, if it is
  78.     printf("\r\nEnter URL = ");
  79.     gets(m_URL);//read url
  80.     printf("\r\nEnter Proxy Server = ");
  81.     gets(m_ProxyServer);//read proxy server
  82.     printf("\r\nEnter Proxy Port = ");
  83.     scanf("%d", &l_ProxyPort);
  84.     getchar();
  85.     printf("\r\nEnter Path = ");
  86.     gets(m_Path);//read path where output file will be saved
  87.     printf("\r\nEnter File Name = ");
  88.     gets(m_SaveToFile);//read file name
  89.     try//for COM library call, try...catch is used to handle exceptions
  90.     {
  91.         IHTTPPtr obj;//take smart pointer object obj for COM library
  92.         CoInitialize(NULL);//Initializes the COM library on the current thread
  93.         obj = IHTTPPtr("AdvHTTP.HTTP");
  94.         obj->PutURL(m_URL);//set url property
  95.         obj->PutProxyServer(m_ProxyServer);//set proxy server property
  96.         obj->put_ProxyPort(l_ProxyPort);//set proxy server port property
  97.         obj->PutPath(m_Path);//set path property
  98.         obj->PutSaveToFile(m_SaveToFile);//set file name property
  99.         IDispatch *p = obj->Get();//call Get method of COM library
  100.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  101.         //GetErrorReason() property of COM will contain error message, for which this error occured
  102.         if (obj->GetIsError() == 1)
  103.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  104.         else
  105.         {
  106.             printf("\r\nSuccessfully Get\r\n");//display successfull message
  107.             IGetPtr getobj;
  108.             p->QueryInterface(IID_IDispatch,(void**)&getobj);//call query interface
  109.             printf("\r\nMime Type = %s\r\nHeader = %s\r\nFile Content = %s\r\n",
  110.                 (char*)getobj->GetMimeType(), (char*)getobj->GetHeader(), (char*)getobj->GetFileContent());
  111.         }
  112.     }
  113.     catch (_com_error &error)//catch exception here
  114.     {
  115.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  116.     }
  117. }
  118.  
  119. /***********************************************************************/
  120. /*Post function: The function is used to perform Post operation of HTTP*/
  121. /***********************************************************************/
  122. void Post()
  123. {
  124.     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
  125.     long l_ProxyPort;//define proxy port, if it is
  126.     printf("\r\nEnter URL = ");
  127.     gets(m_URL);//read url
  128.     printf("\r\nEnter Proxy Server = ");
  129.     gets(m_ProxyServer);//read proxy server
  130.     printf("\r\nEnter Proxy Port = ");
  131.     scanf("%d", &l_ProxyPort);
  132.     getchar();
  133.     printf("\r\nEnter Path = ");
  134.     gets(m_Path);//read path where output file will be saved
  135.     printf("\r\nEnter File Name = ");
  136.     gets(m_SaveToFile);//read file name
  137.     printf("\r\nEnter Form Field Name = ");
  138.     gets(m_FormFieldName);//read form field name to be posted
  139.     printf("\r\nEnter Form Field Value = ");
  140.     gets(m_FormFieldValue);//read value for form field variable
  141.     try//for COM library call, try...catch is used to handle exceptions
  142.     {
  143.         IHTTPPtr obj;//take smart pointer object obj for COM library
  144.         CoInitialize(NULL);//Initializes the COM library on the current thread
  145.         obj = IHTTPPtr("AdvHTTP.HTTP");
  146.         obj->PutURL(m_URL);//set url property
  147.         obj->PutProxyServer(m_ProxyServer);//set proxy server property
  148.         obj->put_ProxyPort(l_ProxyPort);//set proxy server port property
  149.         obj->PutPath(m_Path);//set path property
  150.         obj->PutSaveToFile(m_SaveToFile);//set file name property
  151.         obj->FormField(m_FormFieldName, m_FormFieldValue);//set form field method to post form variables
  152.         // you can also write code here for posting URL variables and file also
  153.         IDispatch *p = obj->Post();//call Post method of COM library
  154.         //call GetIsError() property of COM library and check its value, if its value is 1, then
  155.         //GetErrorReason() property of COM will contain error message, for which this error occured
  156.         if (obj->GetIsError() == 1)
  157.             printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
  158.         else
  159.         {
  160.             printf("\r\nSuccessfully Post\r\n");//display successfull message
  161.             IPostPtr postobj;
  162.             p->QueryInterface(IID_IDispatch,(void**)&postobj);//call query interface
  163.             printf("\r\nMime Type = %s\r\nHeader = %s\r\nFile Content = %s\r\n",
  164.                 (char*)postobj->GetMimeType(), (char*)postobj->GetHeader(), (char*)postobj->GetFileContent());
  165.         }
  166.     }
  167.     catch (_com_error &error)//catch exception here
  168.     {
  169.         printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
  170.     }
  171. }
  172.  
  173.