-1 |
|
Iemand een idee hoe ik een HTTP POST request doe via een C/C++ programma?
ik heb op dit moment dit:
#define WIN_OS
#define _DEBUG_PRINT(X) /* X */
//For commn
#include <iostream>
#include <string>
#include <stdlib.h>
#include <assert.h>
#ifdef LINUX_OS
#include <netdb.h>
#endif
#ifdef WIN_OS
#include <Winsock2.h>
#endif
#define SEND_RQ(MSG) \
/*cout<<send_str;*/ \
send(sock,MSG,strlen(MSG),0);
using namespace std;
//<exe> hostname api parameters
int request (char* hostname, char* api, char* parameters, string& message)
{
#ifdef WIN_OS
{
WSADATA WsaData;
WSAStartup (0x0101, &WsaData);
}
#endif
sockaddr_in sin;
int sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
return -100;
}
sin.sin_family = AF_INET;
sin.sin_port = htons( (unsigned short)80);
struct hostent * host_addr = gethostbyname(hostname);
if(host_addr==NULL) {
_DEBUG_PRINT( cout<<"Unable to locate host"<<endl );
return -103;
}
sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list) ;
_DEBUG_PRINT( cout<<"Port :"<<sin.sin_port<<", Address : "<< sin.sin_addr.s_addr<<endl);
if( connect (sock,(const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1 ) {
_DEBUG_PRINT( cout<<"connect failed"<<endl ) ;
return -101;
}
string send_str;
SEND_RQ("POST ");
SEND_RQ(api);
SEND_RQ(" HTTP/1.0\r\n");
SEND_RQ("Accept: */*\r\n");
SEND_RQ("User-Agent: Mozilla/4.0\r\n");
char content_header[100];
sprintf(content_header,"Content-Length: %d\r\n",strlen(parameters));
SEND_RQ(content_header);
SEND_RQ("Accept-Language: en-us\r\n");
SEND_RQ("Accept-Encoding: gzip, deflate\r\n");
SEND_RQ("Host: ");
SEND_RQ("hostname");
SEND_RQ("\r\n");
SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
//If you need to send a basic authorization
//string Auth = "username:password";
//Figureout a way to encode test into base64 !
//string AuthInfo = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length());
//string sPassReq = "Authorization: Basic " + AuthInfo;
//SEND_RQ(sPassReq.c_str());
SEND_RQ("\r\n");
SEND_RQ("\r\n");
SEND_RQ(parameters);
SEND_RQ("\r\n");
_DEBUG_PRINT(cout<<"####HEADER####"<<endl);
char c1[1];
int l,line_length;
bool loop = true;
bool bHeader = false;
while(loop) {
l = recv(sock, c1, 1, 0);
if(l<0) loop = false;
if(c1[0]=='\n') {
if(line_length == 0) loop = false;
line_length = 0;
if(message.find("200") != string::npos)
bHeader = true;
}
else if(c1[0]!='\r') line_length++;
_DEBUG_PRINT( cout<<c1[0]);
message += c1[0];
}
message="";
if(bHeader) {
_DEBUG_PRINT( cout<<"####BODY####"<<endl) ;
char p[1024];
while((l = recv(sock,p,1023,0)) > 0) {
_DEBUG_PRINT( cout.write(p,l)) ;
p[l] = '\0';
message += p;
}
_DEBUG_PRINT( cout << message.c_str());
} else {
return -102;
}
#ifdef WIN_OS
WSACleanup( );
#endif
return 0;
}
int main(){
string message;
int result = request ("search.yahoo.com", "/search", "fr=FP-pull-web-t&ei=UTF-8&p=test", message);
cout << "Result of query :" << result << endl;
cout << "Server returned :" << message<< endl;
return 0;
}
#define WIN_OS #define _DEBUG_PRINT(X) /* X */ //For commn #include <iostream> #include <string> #include <stdlib.h> #include <assert.h> #ifdef LINUX_OS #include <netdb.h> #endif #ifdef WIN_OS #include <Winsock2.h> #endif #define SEND_RQ(MSG) \ /*cout<<send_str;*/ \ using namespace std; //<exe> hostname api parameters int request (char* hostname, char* api, char* parameters, string& message) { #ifdef WIN_OS { WSADATA WsaData; WSAStartup (0x0101, &WsaData); } #endif int sock = socket (AF_INET, SOCK_STREAM, 0); if (sock == -1) { return -100; } sin.sin_family = AF_INET ; sin.sin_port = htons ( (unsigned short )80); if(host_addr==NULL) { _DEBUG_PRINT( cout<<"Unable to locate host"<<endl ); return -103; } sin.sin_addr .s_addr = *((int *)*host_addr ->h_addr_list) ; _DEBUG_PRINT ( cout <<"Port :"<<sin .sin_port <<", Address : "<< sin.sin_addr .s_addr <<endl ); if( connect (sock ,(const struct sockaddr *)&sin , sizeof(sockaddr_in ) ) == -1 ) { _DEBUG_PRINT( cout<<"connect failed"<<endl ) ; return -101; } string send_str; SEND_RQ("POST "); SEND_RQ(api); SEND_RQ(" HTTP/1.0\r\n"); SEND_RQ("Accept: */*\r\n"); SEND_RQ("User-Agent: Mozilla/4.0\r\n"); char content_header[100]; sprintf(content_header ,"Content-Length: %d\r\n",strlen(parameters )); SEND_RQ(content_header); SEND_RQ("Accept-Language: en-us\r\n"); SEND_RQ("Accept-Encoding: gzip, deflate\r\n"); SEND_RQ("Host: "); SEND_RQ("hostname"); SEND_RQ("\r\n"); SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n"); //If you need to send a basic authorization //string Auth = "username:password"; //Figureout a way to encode test into base64 ! //string AuthInfo = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length()); //string sPassReq = "Authorization: Basic " + AuthInfo; //SEND_RQ(sPassReq.c_str()); SEND_RQ("\r\n"); SEND_RQ("\r\n"); SEND_RQ(parameters); SEND_RQ("\r\n"); _DEBUG_PRINT(cout<<"####HEADER####"<<endl); char c1[1]; int l,line_length; bool loop = true; bool bHeader = false; while(loop) { l = recv(sock, c1, 1, 0); if(l<0) loop = false; if(c1[0]=='\n') { if(line_length == 0) loop = false; line_length = 0; if(message.find("200") != string::npos) bHeader = true; } else if(c1[0]!='\r') line_length++; _DEBUG_PRINT( cout<<c1[0]); message += c1[0]; } message=""; if(bHeader) { _DEBUG_PRINT( cout<<"####BODY####"<<endl) ; char p[1024]; while((l = recv(sock,p,1023,0)) > 0) { _DEBUG_PRINT( cout.write(p,l)) ; p[l] = '\0'; message += p; } _DEBUG_PRINT( cout << message.c_str()); } else { return -102; } #ifdef WIN_OS WSACleanup( ); #endif return 0; } int main(){ string message; int result = request ("search.yahoo.com", "/search", "fr=FP-pull-web-t&ei=UTF-8&p=test", message); cout << "Result of query :" << result << endl; cout << "Server returned :" << message<< endl; return 0; }
maar dit geeft een "aantal" errors
------ Build started: Project: httpPost, Configuration: Debug Win32 ------
Compiling...
httpPost.cpp
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(69) : warning C4244: 'initializing' : conversion from 'SOCKET' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(91) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(92) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(93) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(94) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(95) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(99) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(100) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(101) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(102) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(103) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(104) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(105) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(114) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(115) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(116) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(117) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
Linking...
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)
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)
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)
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)
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)
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)
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)
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)
Debug/httpPost.exe : fatal error LNK1120: 8 unresolved externals
Build log was saved at "file://c:\tmp\Visual Studio Projects\httpPost\Debug\BuildLog.htm"
httpPost - 9 error(s), 17 warning(s)
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
------ Build started: Project: httpPost, Configuration: Debug Win32 ------ Compiling... httpPost.cpp c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(69) : warning C4244: 'initializing' : conversion from 'SOCKET' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(91) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(92) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(93) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(94) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(95) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(99) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(100) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(101) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(102) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(103) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(104) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(105) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(114) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(115) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(116) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data c:\tmp\Visual Studio Projects\httpPost\httpPost.cpp(117) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data Linking... 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) 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) 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) 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) 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) 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) 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) 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) Debug/httpPost.exe : fatal error LNK1120: 8 unresolved externals Build log was saved at "file://c:\tmp\Visual Studio Projects\httpPost\Debug\BuildLog.htm"httpPost - 9 error(s), 17 warning(s) ---------------------- Done ---------------------- Build: 0 succeeded, 1 failed, 0 skipped
|