Reputation: 111
I have a MYSQL database linked to a webpage which, through some PHP code pushes pertinent data using JSON to a blank page. I am trying to grab this data using C++ and sockets and verifying a Key using the URL data.
I have a method of doing this using C# and a simple WebClient().DownloadString but I am having issues translating it to C++.
I have tried using some of the other libs mentioned around stackoverflow but haven't had the luck I was hoping for. Any help would be greatly appreciated.
char* SocketRequest_URL(char* URL, char* Key, int sign, char* Path = "")
{
char *begin = bufferReturn;
char *end = begin + sizeof(bufferReturn);
std::fill(begin, end, 0);
Host = gethostbyname(URL);
SocketAddress.sin_addr.s_addr = *((unsigned long*)Host->h_addr);
SocketAddress.sin_family = AF_INET;
SocketAddress.sin_port = SERVER_PORT;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (connect(Socket, (struct sockaddr *)&SocketAddress, sizeof(SocketAddress)) != CELL_OK) {
return "CONNECTION ERROR";
}
strcpy(RequestBuffer, "GET /");
strcat(RequestBuffer, Path);
char sign_Buf[15];
sprintf(sign_Buf, "%d", sign);
strcat(RequestBuffer, sign_Buf);
strcat(RequestBuffer, "/");
strcat(RequestBuffer, Key);
strcat(RequestBuffer, " HTTP/1.0\r\nHOST: ");
strcat(RequestBuffer, URL);
strcat(RequestBuffer, "\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
strcat(RequestBuffer, "\r\n\r\n");
send(Socket, RequestBuffer, strlen(RequestBuffer), 0);
while (recv(Socket, bufferReturn, 1024, 0) > 0)
{
return bufferReturn;
}
}
Essentially, the page I am trying to pull data from has information pushed to it. I want to grab that data and save it. I can't even seem to get connected to the link.
Upvotes: 0
Views: 603
Reputation: 1102
I would not recommend to use socket API for HTTP/S interactions. Here is a list of the C/C++ libraries/stacks that may be used for that, for example:
Also, you should add if condition for socket API functions and check returning values which could give you a tip about what's happening in your code. And your code returns from the function after first successful call to the recv that is incorrect as you must expect as much calls to recv as needed, until it returns -1.
Upvotes: 1