C++ WinSock Get Data From PHP Page? -
hello i'm quite new using sockets
, not familiar them yet, trying pass string variable
web address
(e.g. www.example.com/index.php?example=stringexample
) , response, example return "test example"
if index.php
looked this:
<?php if($_get['example'] == "stringexample") { echo "test example"; } ?>
here i've tried in c++:
struct sockaddr_in socketaddress; hostent* addr = gethostbyname("www.example.com/index.php?example=stringexample"); int sizeofaddr = sizeof(addr); socketaddress.sin_addr.s_addr = inet_addr(addr->h_name); socketaddress.sin_port = htons(80); socketaddress.sin_family = af_inet; socket connection = socket(af_inet, sock_stream, null); if (connect(connection, (sockaddr*)&addr, sizeofaddr) != 0) { return 0; //failed connect } char buffff[256]; recv(connection, buffff, sizeof(buffff), null); //"test example" stored in buffff
what doing wrong?
btw in case not use libraries
boost
or that. :)
gethostbyname("www.example.com/index.php?example=stringexample");
"www.example.com/index.php?example=stringexample" not valid server name. entire url; server name "www.example.com". gethostbyname()
takes name of server, , not url, , returns ip address. additionally, gethostbyname()
has been obsoleted. new code should use getaddrinfo(3) function, instead.
this http
url. download document via http lot more work connecting socket. establishing socket connection first step in process of downloading document http server. must followed sending valid http request, , receiving http response server.
there many libraries, such curl, implement entire client-side process needed download http document, handle socket connection themselves.
but there's nothing wrong trying implement yourself, either. it's programming excersize.
so, after resolving www.example.com
's ip address, need to
1) connect server's port 80, default http port.
2) send http request "/index.php?example=stringexample".
3) parse http response.
the specification http requests , responses defined rfc 2616, can consult complete documentation of how http requests , responses structured.
Comments
Post a Comment