So sollte es gehen.
Diese Variable _debug und die Funktion SetLog waren von mir, darum konnte es nicht gehen, jetzt sollte es aber. ;)
PHP Code:
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "wininet.lib")
#include <string.h>
#include <winsock2.h>
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <sstream>
using namespace std;
string HTTPRequest(string server, string sSeite = "",string sPost = "",string sCookies = "",string sReferer = "",short modified=0,short WaitForAnswer=1,string StopFound="",bool normal_stop=1,short ContStop=1,string Stop="");
unsigned long GetIP( string Host);
int main()
{
string user="test",pw="test";
string post="user="+user+"&password="+pw+"&clear=true";
string source=HTTPRequest("www.die-stämme.de","/index.php?action=login&server_list=1&show_server_selection=1",post);
}
string HTTPRequest(string server, string sSeite ,string sPost ,string sCookies ,string sReferer ,short modified,short WaitForAnswer,string StopFound,bool normal_stop,short ContStop,string Stop)
{
const int BUFFER_SIZE = 65535;
int sicherheit=0 ;
string request ;
size_t pos;
WSADATA w;
int res = WSAStartup( MAKEWORD( 2,0 ), &w );
if( res != 0 )
{
cout<<"fail 1"<< WSAGetLastError()<<"\n";
return "fail 1";
}
if (sPost == "")
{
request = "GET " +sSeite+ " HTTP/1.1\r\n" ;
}
else
{
request = "POST " +sSeite+ " HTTP/1.1\r\n" ;
}
request+="Host: " ;
request+=server;
request+= "\r\n";
request+="User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)\r\n";
request+="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
request+="Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
request+="Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
request+="Keep-Alive: 300\r\n";
request+="Content-Type: application/x-www-form-urlencoded\r\n" ;
request+="Connection: keep-alive\r\n";
if (modified==1) request+="X-Requested-With: XMLHttpRequest\r\nX-Prototype-Version: 1.6.1\r\n";
if (modified==2) request+="If-None-Match: \"740d1-10ea-4890fad4f1b00\"";
if (sReferer != "")
{
request+= "Referer: " + sReferer +"\r\n" ;
}
if (sCookies != "")
{
while (pos!=string::npos)
{
sicherheit++ ;
if (sicherheit>1000) break;
pos=sCookies.find(" ") ;
if (pos!=string::npos) sCookies.replace(pos,1,"") ;
}
sicherheit=0;
request += "Cookie: " + sCookies + "\r\n";
}
if (sPost != "")
{
char num[7] ;
sprintf(num, "%d", sPost.length()) ;
request+= "Content-Length: ";
request+=num;
request+="\r\n";
}
request+="\r\n" ;
if (sPost != "")
{
request+= sPost;
}
string out;
unsigned int sSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( sSocket == INVALID_SOCKET)
{
cout<<"fail 2"<< WSAGetLastError()<<"\n";
cout<<request<<"\n";
return "fail 2";
}
unsigned long ip = GetIP( server );
SOCKADDR_IN service;
service.sin_family = AF_INET;
service.sin_port = htons( 80 );
service.sin_addr.s_addr = ip;
res = connect( sSocket, (SOCKADDR *)(&service), sizeof( service ) );
if( res == SOCKET_ERROR )
{
cout<<"fail 3"<< ip<< WSAGetLastError()<<"\n";
cout<<request<<"\n";
return "fail 3";
}
send( sSocket, request.c_str(), request.size(), 0 );
if (WaitForAnswer==1)
{
char *buffer = (char *) malloc(BUFFER_SIZE);
while (1)
{
res = recv( sSocket, buffer, BUFFER_SIZE-1, 0);
buffer[res] = '\0';
out += buffer;
if ((res<=0 || out.find("</html>")!=string::npos || out.find("</HTML>")!=string::npos || out.find("/html>")!=string::npos || out.find("/body>")!=string::npos)&& normal_stop==1) break;
if (out.find(StopFound)!=string::npos && StopFound != "") break;
if (out.find("Content-Length: 0")!=string::npos && ContStop==1) break;
if (out.find(Stop)!=string::npos and Stop!="") break;
if (sicherheit>100) break;
sicherheit++ ;
}
delete [] buffer;
}
//closesocket(sSocket);
//shutdown(sSocket, SD_SEND);
WSACleanup();
return out;
} // ==> HTTPRequest(string server, string sSeite ,string sPost ,string sCookies ,string sReferer ,short modified,short WaitForAnswer,string StopFound,short ContStop,string Stop)
unsigned long GetIP( string Host)
{
struct hostent* HomeP;
unsigned long HostIp;
HostIp = inet_addr( Host.c_str() );
if( HostIp == INADDR_NONE )
{
HomeP = gethostbyname( Host.c_str() );
if( !HomeP )
{
return 0;
}
else HostIp = *(unsigned long *)HomeP->h_addr;
}
return HostIp;
} // ==> GetIP( string host)