#include <cstdlib>
#include <iostream>
#include <winsock2.h>
#include <string>
#include <fstream>
using namespace std;
int WSAStart()
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2,0), &wsa);
}
string StringBetween(const string& input, const string& left, const string& right)
{
size_t posLeft, posRight;
posLeft = input.find(left);
posRight = input.find(right);
if (posLeft == string::npos || posRight == string::npos)
{
return ""; // Nicht gefunden
}
posLeft += left.length();
posRight += right.length();
string ergebnis1 = input.substr(posLeft, input.length() - right.length() - left.length());
int formel = (input.find(right) - posLeft);
string ergebnis2 = ergebnis1.substr(0, formel);
return ergebnis2;
}
void gebaus(char array[])
{
for (int i=0;array[i]!=0;i++)
{
cout << array[i];
}
cout << endl;
}
string WSAgetDataGet(string ip, string host, string seite = "", string cookie_str = "")
{
string data = "";
if (cookie_str!="")
{
data = "GET /" + seite + " HTTP/1.0\
"
"Host: " + host + "\
"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16 (.NET CLR 3.5.30729)\
"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\
"
"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\
"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\
"
"Keep-Alive: 300\
"
"Connection: keep-alive\
" + cookie_str +"\
\
";
}
else
{
data = "GET /" + seite + " HTTP/1.0\
"
"Host: " + host + "\
"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16 (.NET CLR 3.5.30729)\
"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\
"
"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\
"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\
"
"Keep-Alive: 300\
"
"Connection: keep-alive\
\
";
}
return data;
}
string itostring(int a)
{
char length[10];
itoa(a, length, 10);
string help;
help.assign(length);
return help;
}
string WSAgetDataPost(string ip, string host, string post, string seite = "", string cookie_str = "")
{
string data = "";
//=======================>länge des Stringes ermitteln
string help = itostring(post.length());
//========================>länge des Stringes ermitteln ENDE
if (cookie_str!="")
{
data = "POST /" + seite + " HTTP/1.0\
"
"Host: " + host + "\
"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16 (.NET CLR 3.5.30729)\
"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\
"
"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\
"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\
"
"Keep-Alive: 300\
"
"Connection: keep-alive\
" + cookie_str + "\
"
"Content-Length: "+help+"\
\
" + post;
}
else
{
data = "POST /" + seite + " HTTP/1.0\
"
"Host: " + host + "\
"
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16 (.NET CLR 3.5.30729)\
"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\
"
"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\
"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\
"
"Keep-Alive: 300\
"
"Connection: keep-alive\
"
"Content-Length: "+help+"\
\
" + post;
}
return data;
}
string WSARequest(string ip, string host, string seite = "", string post = "", string cookie_str = "")
{
SOCKET sock;
if (WSAStart()!=0)
{
cout << "WSA konnte nicht gestartet werden!" << endl;
system("pause");
return "-1";
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock==INVALID_SOCKET)
{
cout << "Socket konnte nicht erstellt werden!" << endl;
system("pause");
return "-1";
}
//================>Ip Adresse konvertieren
char inet_address[30];
strcpy(inet_address, ip.c_str());
//==================>Ip Adresse konvertieren ENDE
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.s_addr = inet_addr(inet_address);
long rc = connect(sock, (SOCKADDR*)&addr, sizeof(sockaddr));
if (rc==SOCKET_ERROR)
{
cout << "SOCKET_ERROR: Error beim Verbinden mit " << ip << endl;
system("pause");
return "-1";
}
else
{
cout << "Verbindung wurde erfolgreich hergestellt!" << endl;
}
//=======================================================================>
//=================>Verbunden mit Server<================================>
//=======================================================================>
string data;
if (post=="") //wenn kein Post benutzt werden soll
{
data = WSAgetDataGet(ip, host, seite, cookie_str);
}
else
{
data = WSAgetDataPost(ip, host, post, seite, cookie_str);
}
//===>Senden anfang
char buffer[data.length()+1];
strcpy(buffer, data.c_str());
cout << data << endl;
rc = send(sock, buffer, sizeof(buffer), 0);
if (rc==SOCKET_ERROR)
{
cout << "Daten konnten nicht versendet werden!" << endl;
system("pause");
return "-1";
}
//=============>SENDEN ENDE
//=======>EMPFANGEN ANFANG
string source = "";
strcpy(buffer, "");
for(;;)
{
rc = recv(sock, buffer, sizeof(buffer), 0);
if(rc == -1)
{
cout << "Fehler: " << WSAGetLastError() << '\n';
break;
}
if(rc == 0)
{
break;
}
source.append(buffer, rc);
}
return source;
}
int main(int argc, char *argv[])
{
system("pause");
string source = WSARequest("195.244.251.165", "www.castlefight.de");
string ssid = StringBetween(source, "Set-Cookie: ssid=", "; path");
string cookie_str = "Cookie: ssid=" + ssid + "; __utma=165185581.1485780560.1262776768.1262776768.1262779583.2; __utmc=165185581; __utmz=165185581.1262776768.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=165185581.1.10.1262779583";
cout << cookie_str << endl;
source = WSARequest("195.244.251.165", "s3.castlefight.de", "login.php", "sid=" + ssid + "&server=s3.castlefight.de&username=testman123&password=test123&passwordlayer=Passwort&send=Einloggen", cookie_str);
//=====>in datei schreiben
fstream f;
f.open("hello.html");
f << source << endl;
f.close();
system("PAUSE");
return EXIT_SUCCESS;
}