col metodo GET ci sono riuscito ed essendo mia la pagina php che vado a visitare ho cambiato POST in GET pero vorrei riuscire a farlo anche col POST metto il codice che sto usando per chi è interessato, l'ho provato su window 7 e su debian e urka come vuorka
tcpclient.h
codice:
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#endif
#include <iostream>
#include <string>
class tcpClient {
private:
int sock;
std::string address;
int port;
struct sockaddr_in server;
public:
tcpClient();
~tcpClient();
bool conn(std::string, int);
bool send_data(std::string);
std::string receive(int);
std::string get(std::string, std::string);
//std::string post(std::string, std::string);
};
#endif
tcpclient.cpp
codice:
#include "tcpclient.h"
tcpClient::tcpClient() {
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
return;
}
#endif
sock = -1;
port = 0;
address = "";
}
tcpClient::~tcpClient() {
#ifdef _WIN32
WSACleanup();
#endif
}
bool tcpClient::conn(std::string address , int port) {
if(sock == -1) {
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1) {
return false;
}
}
if(inet_addr(address.c_str()) == -1) {
struct hostent *he;
struct in_addr **addr_list;
if ( (he = gethostbyname( address.c_str() ) ) == NULL) {
return false;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(int i = 0; addr_list[i] != NULL; i++) {
server.sin_addr = *addr_list[i];
break;
}
} else {
server.sin_addr.s_addr = inet_addr( address.c_str() );
}
server.sin_family = AF_INET;
server.sin_port = htons( port );
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) {
return false;
}
return true;
}
bool tcpClient::send_data(std::string data) {
if( send(sock , data.c_str() , strlen( data.c_str() ) , 0) > 0) {
return true;
}
}
std::string tcpClient::receive(int bSize=512) {
char buffer[bSize];
std::string reply;
if( recv(sock , buffer , sizeof(buffer) , 0) > 0) {
reply = buffer;
return reply;
}
}
std::string extract_body(std::string str) {
int pos = str.find_last_of("\r\n\r\n");
return str.substr(pos);
}
std::string tcpClient::get(std::string url, std::string page) {
std::string pHeader;
pHeader += "GET /" + page + " HTTP/1.1\r\n";
pHeader += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
pHeader += "Accept-Encoding: gzip, deflate\r\n";
pHeader += "Accept-Language: en-US,en;q=0.5";
pHeader += "Connection: keep-alive\r\n";
pHeader += "Host: " + url +"\r\n";
pHeader += "Upgrade-Insecure-Requests: 1\r\n";
pHeader += "User-Agent: test\r\n\r\n";
if (conn(url, 80) && (send_data(pHeader))) {
return extract_body(receive(1024));
}
}
main.cpp
codice:
#include "tcpclient.h"
#include <iostream>
#include <string>
int main() {
tcpClient c;
std::string host = "www.sito.it";
std::string request = "prova.php?user-name=nome&user-password=pass";
std::cout << c.get(host, request);
return 0;
}
su window solo serve -lws2_32
codice:
>g++ tcpclient.cpp main.cpp -lws2_32 -o nomefile
sul debian invece mi da problemi strlen ho dovuto aggiungere anche string.h
codice:
>g++ tcpclient.cpp main.cpp -o nomefile
spero che qualcuno pratico si faccia avanti e mi spiega come fare per il metodo POST e magari anche qualche consiglio su come l'avrebbe impostata lui questa classe
ad esempio extract_body è una funzione friend se cosi la vogliamo chiamare.. non l'ho messa nella classe o forse dovevo metterla tra i metodi privati..
prova.php per completezza
codice:
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/PersonalAgent/i',$browser)) {
if (count($_GET) > 0) {
$userName = trim(filter_var($_GET['user-name'], FILTER_SANITIZE_STRING));
$userPassword = trim(filter_var($_GET['user-password'], FILTER_SANITIZE_STRING));
echo $userName . ", " . $userPassword;
} else { echo "no get"; }
} else { echo "no agent"; }
?>
P.S.: ho postato tutto quanto anche per presentarmi e per dirvi che non sono uno di quelli che rompe le scatole che vuole che le cose glie le facciate voi.. se chiedo aiuto vuol dire che ho googlato fino allo stremo