GET! GOT! GOTCHA! I still don't Get it...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 11
Reputation: Tester2 is an unknown quantity at this point 
Solved Threads: 0
Tester2 Tester2 is offline Offline
Newbie Poster

GET! GOT! GOTCHA! I still don't Get it...

 
0
  #1
Feb 6th, 2005
I am one of the many people trying to grasp something new in this forum. I come here, because it seems to frequented by C++ experts, who actually try to respond.

I am trying to learn how to program an HTTP protocol based text file download. I have searched far and wide on the internet, and find both commercial and non-commercial code to provide internet functionality. I have not found a single, Borland Builder based, windows socket and http tutorial.

I did download one of the free, non-commercial files, and it did compile and run on Borland Builder 6.0 with just a few changes. I believe it has a socket, because the RichText box control I am using fills with various obscure facts about my internet service provider (including its usual $9.95 per month advertisement), all in text, when I activate the code that was said to perform an HTTP based POST. I think that means that the code was able to access a socket. I'm not as sure that it was able to create the socket, as I already had internet explorer opened to a web site and was connected.

IF YOU'VE READ THIS FAR, then perhaps I should note that what I ultimately need to do is to access a password protected directory, and download a text file using http protocol. I was told this was both possible and none too difficult. (I was at my limits getting the file I downloaded to compile under Borland Builder.) I did search for and purchase multiple texts on internet programming, but all via mail. The local bookstores and college library have nothing in them describing C++ programming of HTTP commands, I've spent days looking before I came here. I am attaching some code. It is supposed to accomplish an internet POST. I understand that is useful for chat rooms and a few other things, but not for my needs at present. To be perfectly straightforward, much of what this code does is Greek to me. I downloaded it to see if it could be made to work, as a roadmap to a means by which to get a socket connection. Step two is trying to figure out how to convert the POST function to a GET function that will download a text file from a password protected directory. (I honestly don't know, in the context of this code, what it is supposed to return, or why I received the info I did from an internet service provider whose url I wasn't trying to use.)

I'll post the code I downloaded. If anyone can get me to the point where this code will enable me to access a password protected directory, we'll call it:

"http://mysite.hostserver.com/secret"

with user name: myname

and password: mypassword,

and download file: mytextfile.txt using http,

that would be, as the waitress on the television commercial says, "great".

The code I was using is based upon Windows sockets and winsock2.h. I don't want to change this. I just want it to work. Any help would be much appreciated.

BORLAND BUILDER 6.0 CODE FOLLOWS:

  1. /*
  2.  * http_post.cpp
  3.  *
  4.  * by Uday Chitragar - 2004/Dec/01
  5.  *
  6.  * This software is provided 'as-is', without any express or implied
  7.  * warranty. In no event will the authors be held liable for any
  8.  * damages arising from the use of this software.
  9.  *
  10.  * Permission is granted to anyone to use this software for any
  11.  * purpose, including commercial applications, and to alter it and
  12.  * redistribute it freely, subject to the following restrictions:
  13.  *
  14.  * 1. The origin of this software must not be misrepresented; you must
  15.  * not claim that you wrote the original software. If you use this
  16.  * software in a product, an acknowledgment in the product documentation
  17.  * would be appreciated but is not required.
  18.  *
  19.  * 2. Altered source versions must be plainly marked as such, and
  20.  * must not be misrepresented as being the original software.
  21.  *
  22.  * 3. This notice may not be removed or altered from any source
  23.  * distribution.
  24.  *
  25.  * */
  26.  
  27. /*
  28.  * Notes:
  29.  * This source demonstrates sending HTTP POST request to webserver from C++
  30.  * This uses sockets hence can be compiled on Linux, UNIX, Win
  31.  */
  32.  
  33. //#define LINUX_OS
  34. #define WIN_OS
  35. #define _DEBUG_PRINT(X) /* X */
  36.  
  37. //For commn
  38. #include <iostream.h>
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include <assert.h>
  42.  
  43. //#ifdef LINUX_OS
  44. // #include <netdb.h>
  45. //#endif
  46.  
  47. //#ifdef WIN_OS
  48. // #include <Winsock2.h>
  49. //#endif
  50.  
  51.  
  52. #define SEND_RQ(MSG) \
  53. /*cout<<send_str;*/ \
  54. send(sock,MSG,strlen(MSG),0);
  55.  
  56.  
  57. using namespace std;
  58. //<exe> hostname api parameters
  59. int request (char* hostname, char* api, char* parameters, string& message)
  60. {
  61.  
  62. #ifdef WIN_OS
  63. {
  64. WSADATA WsaData;
  65. WSAStartup (0x0101, &WsaData);
  66. }
  67. #endif
  68. struct sockaddr_in {
  69. short sin_family;
  70. u_short sin_port;
  71. struct in_addr sin_addr;
  72. char sin_zero[8];
  73. };
  74. sockaddr_in sin;
  75. int sock = socket (AF_INET, SOCK_STREAM, 0);
  76. if (sock == -1) {
  77. return -100;
  78. }
  79. sin.sin_family = AF_INET;
  80. sin.sin_port = htons( (unsigned short)80);
  81.  
  82. struct hostent * host_addr = gethostbyname(hostname);
  83. if(host_addr==NULL) {
  84. _DEBUG_PRINT( cout<<"Unable to locate host"<<endl );
  85. return -103;
  86. }
  87. sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
  88. _DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);
  89.  
  90. if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
  91. _DEBUG_PRINT( cout<<"connect failed"<<endl ) ;
  92. return -101;
  93. }
  94.  
  95. string send_str;
  96.  
  97. SEND_RQ("POST ");
  98. SEND_RQ(api);
  99. SEND_RQ(" HTTP/1.0\r\n");
  100. SEND_RQ("Accept: */*\r\n");
  101. SEND_RQ("User-Agent: Mozilla/4.0\r\n");
  102.  
  103. char content_header[100];
  104. sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
  105. SEND_RQ(content_header);
  106. SEND_RQ("Accept-Language: en-us\r\n");
  107. SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
  108. SEND_RQ("Host: ");
  109. SEND_RQ("hostname");
  110. SEND_RQ("\r\n");
  111. SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
  112.  
  113. //If you need to send a basic authorization
  114. //NOTE: IS THIS TO GET
  115. // PAST YOUR OWN SITE'S ADMINISTRATOR PASSWORD, OR THE
  116. // DIRECTORY PASSWORD? I NEED TO GET PAST MY DIRECTORY
  117. // PROTECTING PASSWORD.
  118. //(USING NON-PASSWORD PROTECTED DIRECTORY
  119. // ISN'T AN OPTION).
  120. //string Auth = "mysite:mypassword";
  121. //string AuthInfo = Auth;
  122. //Figureout a way to encode test into base64 ! WHAT????
  123. //string AuthInfo = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
  124. //string sPassReq = "Authorization: Basic " + AuthInfo;
  125. //SEND_RQ(sPassReq.c_str());
  126.  
  127. SEND_RQ("\r\n");
  128. SEND_RQ("\r\n");
  129. SEND_RQ(parameters);
  130. SEND_RQ("\r\n");
  131.  
  132. _DEBUG_PRINT(cout<<"####HEADER####"<<endl);
  133. char c1[1];
  134. int l,line_length;
  135. bool loop = true;
  136. bool bHeader = false;
  137.  
  138. while(loop) {
  139. l = recv(sock, c1, 1, 0);
  140. if(l<0) loop = false;
  141. if(c1[0]=='\n') {
  142. if(line_length == 0) loop = false;
  143.  
  144. line_length = 0;
  145. if(message.find("200") != string::npos)
  146. bHeader = true;
  147.  
  148. }
  149. else if(c1[0]!='\r') line_length++;
  150. _DEBUG_PRINT( cout<<c1[0]);
  151. message += c1[0];
  152. }
  153.  
  154. message="";
  155. if(bHeader) {
  156.  
  157. _DEBUG_PRINT( cout<<"####BODY####"<<endl) ;
  158. char p[1024];
  159. while((l = recv(sock,p,1023,0)) > 0) {
  160. _DEBUG_PRINT( cout.write(p,l)) ;
  161. p[l] = '\0';
  162. message += p;
  163. }
  164.  
  165. _DEBUG_PRINT( cout << message.c_str());
  166. } else {
  167. return -102;
  168. }
  169.  
  170.  
  171. #ifdef WIN_OS
  172. WSACleanup( );
  173. #endif
  174.  
  175. return 0;
  176. }
  177.  
  178.  
  179. void __fastcall TForm1::Button1Click(TObject *Sender)
  180. {
  181. string message;
  182. char* mysite = "www.netzero.com";
  183. char* postapi = "/post_url.pl";
  184. char* myparams = "search=hello&date=todat";
  185. //Following line originally used "string& message". I don't know what
  186. //that was supposed to accomplish, but what follows
  187. //was what I was able to get to compile, with "string message" instead.
  188. int errornumberreturned = request(mysite, postapi, myparams, message);
  189. const char* mymessage = message.c_str();
  190. RichEdit1->Text= mymessage;
  191. RichEdit2->Text = errornumberreturned;
  192. //errornumberreturned was 0 -> never got to functions to return error code??
  193. // message contains response! WELL, it contained a response.
  194. }
  195. //---------------------------------------------------------------------------
Last edited by alc6379; Feb 8th, 2005 at 12:27 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC