login  Naam:   Wachtwoord: 
Registreer je!
 Forum

[C/C++] HTTP POST

Offline Ontani - 09/02/2006 17:17 (laatste wijziging 09/02/2006 17:39)
Avatar van OntaniGouden medailleGouden medailleGouden medailleGouden medaille

-1
Iemand een idee hoe ik een HTTP POST request doe via een C/C++ programma?

ik heb op dit moment dit:

  1. #define WIN_OS
  2. #define _DEBUG_PRINT(X) /* X */
  3.  
  4. //For commn
  5. #include <iostream>
  6. #include <string>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9.  
  10. #ifdef LINUX_OS
  11. #include <netdb.h>
  12. #endif
  13.  
  14. #ifdef WIN_OS
  15. #include <Winsock2.h>
  16. #endif
  17.  
  18.  
  19. #define SEND_RQ(MSG) \
  20. /*cout<<send_str;*/ \
  21. send(sock,MSG,strlen(MSG),0);
  22.  
  23.  
  24. using namespace std;
  25. //<exe> hostname api parameters
  26. int request (char* hostname, char* api, char* parameters, string& message)
  27. {
  28.  
  29. #ifdef WIN_OS
  30. {
  31. WSADATA WsaData;
  32. WSAStartup (0x0101, &WsaData);
  33. }
  34. #endif
  35.  
  36. sockaddr_in sin;
  37. int sock = socket (AF_INET, SOCK_STREAM, 0);
  38. if (sock == -1) {
  39. return -100;
  40. }
  41. sin.sin_family = AF_INET;
  42. sin.sin_port = htons( (unsigned short)80);
  43.  
  44. struct hostent * host_addr = gethostbyname(hostname);
  45. if(host_addr==NULL) {
  46. _DEBUG_PRINT( cout<<"Unable to locate host"<<endl );
  47. return -103;
  48. }
  49. sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
  50. _DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);
  51.  
  52. if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
  53. _DEBUG_PRINT( cout<<"connect failed"<<endl ) ;
  54. return -101;
  55. }
  56.  
  57. string send_str;
  58.  
  59. SEND_RQ("POST ");
  60. SEND_RQ(api);
  61. SEND_RQ(" HTTP/1.0\r\n");
  62. SEND_RQ("Accept: */*\r\n");
  63. SEND_RQ("User-Agent: Mozilla/4.0\r\n");
  64.  
  65. char content_header[100];
  66. sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
  67. SEND_RQ(content_header);
  68. SEND_RQ("Accept-Language: en-us\r\n");
  69. SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
  70. SEND_RQ("Host: ");
  71. SEND_RQ("hostname");
  72. SEND_RQ("\r\n");
  73. SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
  74.  
  75. //If you need to send a basic authorization
  76. //string Auth = "username:password";
  77. //Figureout a way to encode test into base64 !
  78. //string AuthInfo = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
  79. //string sPassReq = "Authorization: Basic " + AuthInfo;
  80. //SEND_RQ(sPassReq.c_str());
  81.  
  82. SEND_RQ("\r\n");
  83. SEND_RQ("\r\n");
  84. SEND_RQ(parameters);
  85. SEND_RQ("\r\n");
  86.  
  87. _DEBUG_PRINT(cout<<"####HEADER####"<<endl);
  88. char c1[1];
  89. int l,line_length;
  90. bool loop = true;
  91. bool bHeader = false;
  92.  
  93. while(loop) {
  94. l = recv(sock, c1, 1, 0);
  95. if(l<0) loop = false;
  96. if(c1[0]=='\n') {
  97. if(line_length == 0) loop = false;
  98.  
  99. line_length = 0;
  100. if(message.find("200") != string::npos)
  101. bHeader = true;
  102.  
  103. }
  104. else if(c1[0]!='\r') line_length++;
  105. _DEBUG_PRINT( cout<<c1[0]);
  106. message += c1[0];
  107. }
  108.  
  109. message="";
  110. if(bHeader) {
  111.  
  112. _DEBUG_PRINT( cout<<"####BODY####"<<endl) ;
  113. char p[1024];
  114. while((l = recv(sock,p,1023,0)) > 0) {
  115. _DEBUG_PRINT( cout.write(p,l)) ;
  116. p[l] = '\0';
  117. message += p;
  118. }
  119.  
  120. _DEBUG_PRINT( cout << message.c_str());
  121. } else {
  122. return -102;
  123. }
  124.  
  125.  
  126. #ifdef WIN_OS
  127. WSACleanup( );
  128. #endif
  129.  
  130. return 0;
  131. }
  132.  
  133.  
  134. int main(){
  135. string message;
  136. int result = request ("search.yahoo.com", "/search", "fr=FP-pull-web-t&ei=UTF-8&p=test", message);
  137. cout << "Result of query :" << result << endl;
  138. cout << "Server returned :" << message<< endl;
  139. return 0;
  140. }


maar dit geeft een "aantal" errors
  1. ------ Build started: Project: httpPost, Configuration: Debug Win32 ------
  2.  
  3. Compiling...
  4. httpPost.cpp
  5. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(69) : warning C4244: 'initializing' : conversion from 'SOCKET' to 'int', possible loss of data
  6. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(91) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  7. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(92) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  8. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(93) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  9. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(94) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  10. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(95) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  11. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(99) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  12. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(100) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  13. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(101) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  14. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(102) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  15. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(103) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  16. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(104) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  17. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(105) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  18. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(114) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  19. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(115) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  20. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(116) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  21. c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(117) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
  22. Linking...
  23. httpPost.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  24. httpPost.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  25. httpPost.obj : error LNK2019: unresolved external symbol __imp__send@16 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  26. httpPost.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  27. httpPost.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  28. httpPost.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  29. httpPost.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  30. httpPost.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "int __cdecl request(char *,char *,char *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?request@@YAHPAD00AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
  31. Debug/httpPost.exe : fatal error LNK1120: 8 unresolved externals
  32.  
  33. Build log was saved at "file://c:\tmp\Visual Studio Projects\httpPost\Debug\BuildLog.htm"
  34. httpPost - 9 error(s), 17 warning(s)
  35.  
  36.  
  37. ---------------------- Done ----------------------
  38.  
  39. Build: 0 succeeded, 1 failed, 0 skipped

2 antwoorden

Gesponsorde links
Offline Stijn - 09/02/2006 21:28
Avatar van Stijn PHP expert ok, ik heb het aan een C++ freak gevraagd en hij zegt dat je volgende code moet toevoegen aan je C++ code:
  1. #pragma comment(lib, "wsock32.lib")


groeten
stijn
Offline Ontani - 09/02/2006 22:07
Avatar van Ontani Gouden medailleGouden medailleGouden medailleGouden medaille

-1
dat blijkt inderdaad te lukken bedankt
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.198s