QHttp méthode GET et POST
Bonjour,
j'ai essayer de faire ma connexion sur mon site mais j'ai un petit problème très gênant !
Ma méthode GET de ma fonction fonctionne parfaitement bien ! Mais impossible de faire marcher la méthode POST. Et j'ai très besoins de d'utiliser POST...
J'ai pas trouvé beaucoup de doc sur la méthode POST donc ça viens aussi surement de là.
Voilà mon fichier avec la méthode GET qui fonctionne et espérant une aide.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#ifndef CLASSHTTP_H
#define CLASSHTTP_H
#define PORT 80
#include <winsock2.h>
#include <iostream>
#include <fstream>
using namespace std;
class Http
{
private:
SOCKET sock;
int erreur;
WSADATA WSAData;
SOCKADDR_IN sin;
bool connected;
string host;
public:
Http()
{
erreur = WSAStartup(MAKEWORD(2,0), &WSAData);
if(erreur)
{
cerr << "Error" << endl;
connected = false;
return;
}
connected = false;
}
void start(string _host,string _hh = "NULL")
{
if(_hh == "NULL")
{
int f = _host.find("www.");
if(f >= 0)
{
host = _host;
}
else
{
host = "www.";
host+= _host;
}
}
else
{
host = _hh;
}
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr(getIp(_host).c_str() );
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
if(connect(sock, (SOCKADDR *)&sin, sizeof(sin)) != SOCKET_ERROR)
{
connected = true;
}
else
{
connected = false;
}
}
bool isConnected()
{
return connected;
}
bool Send(string chaine, bool GP = true)
{
if(!connected)
return false;
string requete;
if(GP)
requete = "GET ";
else
requete = "POST ";
requete+=chaine;
requete+=" HTTP/1.1\r\nHost:";
requete+=host;
requete+="\r\nContent-type: application/x-www-form-urlencoded\r\nAccept : */*\r\n\r\n";
if(send(sock,requete.c_str(),requete.size(), 0) == SOCKET_ERROR)
{
connected = false;
return false;
}
return true;
}
string Recv(int size = (2048*20))
{
if(!connected)
return "NOT CONNECTED";
char chaine[size];
if(recv(sock,chaine,size,0) != SOCKET_ERROR)
return string(chaine);
connected = false;
return "ERROR";
}
private:
string getIp(string name)
{
struct in_addr MyAddress;
struct hostent *host;
host = gethostbyname(name.c_str());
if(host != NULL)
{
memcpy( &MyAddress.s_addr, host->h_addr_list[0], sizeof( MyAddress.s_addr ) );
return inet_ntoa( MyAddress );
}
return name;
}
};
#endif |
Et je l'apel dans mon fichier tool.cpp comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
void Tool::on_pushButton_connection_clicked()
{
QString page = "/live.php?req_username=";
page+=uiConfig.lineEdit_2->text();
page+="&req_password=";
page+=QCryptographicHash::hash(uiConfig.lineEdit->text().toUtf8(),QCryptographicHash::Md5).toHex();
page+="&viens_de_site_live";
Http t;// variable de la class Http
t.start("site.fr");
if(t.isConnected())
{
t.Send(page.toStdString());
string req = "/";
cout << t.Send(req) << endl; // Fonction pour poster une requête ici on demande l'index
string tm = t.Recv();// Fonction pour recevoir après la requête d'envoi précédente
cout << tm << endl;
QString pseudo = t.Recv().c_str();
int debut_pseudo = pseudo.indexOf("<pseudo>") + QString("<pseudo>").size();
int taille_pseudo = pseudo.indexOf("</pseudo>") - debut_pseudo;
pseudo = pseudo.mid(debut_pseudo,taille_pseudo);
QMessageBox::information(0, tr("Votre Pseudo est :"),pseudo);
QString mdp = t.Recv().c_str();
int debut_mdp = mdp.indexOf("<mdp>") + QString("<mdp>").size();
int taille_mdp = mdp.indexOf("</mdp>") - debut_mdp;
mdp = mdp.mid(debut_mdp,taille_mdp);
QMessageBox::information(0, tr("Votre mot de passe est :"),mdp);
}
else
{
QMessageBox::warning(0, tr("Erreur de connection"), tr("La connection n'a pu être établie avec le serveur"));
}
} |
Merci !