PDA

View Full Version : Submitting HTTP POST from C/C++?


Alex Vincent
03-21-2005, 08:51 PM
I know it's possible. Otherwise, such great products such as Mozilla, Opera, Amaya, even Internet Explorer, wouldn't work...

http://www.codeguru.com/Cpp/I-N/internet/http/article.php/c8813/

I found this via a Google search, but unfortunately, it doesn't compile...

Before you ask, yes, I did turn off the #define LINUX_OS and turn on the #define WIN_OS bits.

I need this for work, as we're trying to determine where we spend our time.

Unit
03-21-2005, 09:03 PM
What errors do you get? what compiler are you using?

You might want to include windows.h at the top and see if that helps.

Alex Vincent
03-21-2005, 10:22 PM
http_post\http_post.cpp(73): error C2065: 'sockaddr_in' : undeclared identifier
http_post\http_post.cpp(73): error C2146: syntax error : missing ';' before identifier 'sin'
http_post\http_post.cpp(73): error C2065: 'sin' : undeclared identifier
http_post\http_post.cpp(74): error C2065: 'AF_INET' : undeclared identifier
http_post\http_post.cpp(74): error C2065: 'SOCK_STREAM' : undeclared identifier
http_post\http_post.cpp(74): error C3861: 'socket': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(78): error C2228: left of '.sin_family' must have class/struct/union type
http_post\http_post.cpp(78): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(78): error C3861: 'AF_INET': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(79): error C2228: left of '.sin_port' must have class/struct/union type
http_post\http_post.cpp(79): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(79): error C3861: 'htons': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(81): error C3861: 'gethostbyname': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(83): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(86): error C2228: left of '.sin_addr' must have class/struct/union type
http_post\http_post.cpp(86): error C2228: left of '.s_addr' must have class/struct/union type
http_post\http_post.cpp(86): error C2027: use of undefined type '(char *,char *,char *,std::string &)request::hostent'
http_post\http_post.cpp(86): error C2227: left of '->h_addr_list' must point to class/struct/union
http_post\http_post.cpp(86): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(87): error C2228: left of '.sin_port' must have class/struct/union type
http_post\http_post.cpp(87): error C2228: left of '.sin_addr' must have class/struct/union type
http_post\http_post.cpp(87): error C2228: left of '.s_addr' must have class/struct/union type
http_post\http_post.cpp(87): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(87): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(87): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(89): error C2070: ''unknown-type'': illegal sizeof operand
http_post\http_post.cpp(89): error C3861: 'connect': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(89): error C3861: 'sin': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(89): error C3861: 'sockaddr_in': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(90): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(96): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(97): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(98): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(99): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(100): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(104): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(105): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(106): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(107): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(108): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(109): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(110): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(119): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(120): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(121): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(122): error C3861: 'send': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(124): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(131): error C3861: 'recv': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(142): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(149): error C3861: '_DEBUG_PRINT': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(151): error C3861: 'recv': identifier not found, even with argument-dependent lookup
http_post\http_post.cpp(151): fatal error C1903: unable to recover from previous error(s); stopping compilation


Visual C++ .NET. No luck with "include <windows.h>".

I realize one of the rules is "Fix your first error, and half your other errors go away", but here I'm in unfamiliar territory.

Unit
03-21-2005, 10:35 PM
This certainly seems like a missing includes/include order problem.

I think VC++ .Net uses the new header files for winsock.

try including these right at the top.
winsock2.h
ws2tcpip.h
windows.h
in that order. be sure to remove winsock2.h that's already there down the include list.

Alex Vincent
03-21-2005, 11:01 PM
No change. :confused:

UPDATE: Actually, when I go to my command line and execute:
cl /EHsc http_post.cpp

I get only one error:

http_post.cpp(169) : error C2275: 'std::string' : illegal use of this type as an expression
R:\msvc71.net\Vc7\include\xstring(1562) : see declaration of 'std::string'
http_post.cpp(169) : error C2078: too many initializers


This is after I set up my Mozilla build environment.

Unit
03-22-2005, 07:19 AM
Hmm, this program seems to have been written in haste. I am guessing that no compiler can compile this properly. Remove the highlights and try again. you may need to specify winsock library location to the linker.

int main(){
string message;
int request ("www.somesite.com", "/post_url.pl", "search=hello&date=todat", string& message);
// message contains response!
}

Edit: With this change, I get a nice output

Port :20480, Address : 1661429314
####HEADER####
HTTP/1.0 501 Not Implemented
Content-Type: text/html
Server: GWS/2.1
Content-Length: 1236
Date: Tue, 22 Mar 2005 06:36:08 GMT
Connection: Close

####BODY####


<html><head>
<title>501 Not Implemented</title>
<style><!--
body {font-family: arial,sans-serif}
div.nav {margin-top: 1ex}
div.nav A {font-size: 10pt; font-family: arial,sans-serif}
span.nav {font-size: 10pt; font-family: arial,sans-serif; font-weight: bold}
div.nav A,span.big {font-size: 12pt; color: #0000cc}
div.nav A {font-size: 10pt; color: black}
A.l:link {color: #6f6f6f}
A.u:link {color: green}
//--></style>

Alex Vincent
03-23-2005, 02:11 AM
I don't think this thing likes me:


http_post.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?$al
locator@D@2@@std@@@Z)
http_post.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?$allocat
or@D@2@@std@@@Z)
http_post.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?$allocat
or@D@2@@std@@@Z)
http_post.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?$allo
cator@D@2@@std@@@Z)
http_post.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)
http_post.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?$allocat
or@D@2@@std@@@Z)
http_post.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?$alloc
ator@D@2@@std@@@Z)
http_post.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?$al
locator@D@2@@std@@@Z)
http_post.exe : fatal error LNK1120: 8 unresolved externals


:rolleyes:

UPDATE: My boss (who knows Visual C++ .NET quite well) came over and finally bailed me out. I was missing a library -- and your guesses were largely right. Thanks!!!

jmcassis
09-22-2005, 05:07 PM
Hi!!
I am trying to compile this code too, and I got the same erros. What library did you include?? I am using Visual Studio C++ .NET and Dev-C++. Both get those unresolved external symbol.

Thanks very much!!!

rockey
08-26-2006, 08:05 PM
i needed to make some corrections in that code to get it run properly

1. to call the method request i use syntax as below:

string message;
string* mes = &message;
request(hostname,path,params,*mes);

note that port of the host is hardcoded there;)

2. //SEND_RQ("\r\n");
SEND_RQ(parameters);

i removed one line here - i had some problems with sending params