Winsock + php -> Seite laden



  • Ich versuch eine Seite, wie zb http://www.c-plusplus.net/forum/viewforum.php?f=5 zu laden. Mit normalen Seiten klappt das, doch mitsolchen parameterisierten php Seitenbekomme ich immer eine Fehler Seite zurück

    #include <string>
    #include <iostream>
    #include <stdio.h>
    #include <windows.h>
    #include <winsock.h>
    using namespace std;
    
    #define DEBUG 1
    #define PUFFERGROESSE 1024
    
    class CHttpDownload {
        public:
            string host;
            int port;
            string url;
            string zieldatei;
            string antwortaufrufer;
    
            // ohne Proxy
            CHttpDownload(string url, string zieldatei) {
                int pos;
                if ((pos=url.find("http://"))>-1) {
                    host=url.substr(pos+7,url.length());
                    if ((pos=host.find("/"))>-1) {
                        host=host.substr(0,pos);
                    }
                }
                else {
                    if (DEBUG==1) printf("*** CHttpDownload: die URL muss mit http:// anfangen!\n");
                    return;
                }
                if (DEBUG==1) printf("CHttpDownload: Der Host ist: %s\n",host.c_str());
                this->host=host;
                this->port=80;
                this->url=url;
                this->zieldatei=zieldatei;
                httpDownload();
            }
    
            // mit Proxy
            CHttpDownload(string url, string zieldatei, string proxy, int port) {
                this->host=proxy;
                this->port=port;
                this->url=url;
                this->zieldatei=zieldatei;
                httpDownload();
            }
    
            ~CHttpDownload() {
            }
    
            void httpDownload () {
                struct sockaddr_in server;
                struct hostent *host_info;
                unsigned long addr;
                int bzaehler;
                int sock;
                char buffer[PUFFERGROESSE];
    
                int pos;
    
                /* Initialisiere TCP für Windows ("winsock") */
                short wVersionRequested;
                WSADATA wsaData;
                wVersionRequested=MAKEWORD(1,1);
                if (WSAStartup(wVersionRequested,&wsaData)!=0) {
                    if (DEBUG==1) printf("*** CHttpDownload: Konnte Windows-Sockets nicht initialisieren! Fehlercode: %d\n",WSAGetLastError());
                    WSACleanup();
                    return;
                }
    
                /* Erzeuge das Socket */
                sock=socket(PF_INET,SOCK_STREAM,0);
                if (sock<0) {
                    if (DEBUG==1) printf("*** CHttpDownload: Konnte Socket nicht erzeugen! Fehlercode: %d\n",WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    return;
                }
    
                /* Erzeuge die Socketadresse des Servers
                   Sie besteht aus Typ, IP-Adresse und Portnummer */
                memset(&server,0,sizeof(server));
                if ((addr=inet_addr(host.c_str()))!=INADDR_NONE) {
                    /* argv[1] ist eine numerische IP-Adresse */
                    memcpy((char *)&server.sin_addr,&addr,sizeof(addr));
                }
                else {
                    /* Wandle den Servernamen in eine IP-Adresse um */
                    host_info=gethostbyname(host.c_str());
                    if (NULL==host_info) {
                        if (DEBUG==1) printf("*** CHttpDownload: Host %s ist unbekannt! Fehlercode: %d\n",host.c_str(),WSAGetLastError());
                        closesocket(sock);
                        WSACleanup();
                        return;
                    }
                    memcpy((char *)&server.sin_addr,host_info->h_addr,host_info->h_length);
                }
    
                server.sin_family=AF_INET;
                server.sin_port=htons(port);
    
                /* Baue die Verbindung zum Server auf */
                if (connect(sock,(struct sockaddr*)&server,sizeof(server))<0) {
                    if (DEBUG==1) printf("*** CHttpDownload: Konnte Host %s nicht erreichen! Fehlercode: %d\n",host.c_str(),WSAGetLastError());
                    closesocket(sock);
                    WSACleanup();
                    return;
                }
    
                HANDLE hZieldatei=CreateFile(zieldatei.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
                if (hZieldatei==NULL) {
                    if (DEBUG==1) printf("*** CHttpDownload: Konnte Zieldatei %s nicht erstellen! Fehlercode: %d\n",zieldatei.c_str(),GetLastError());
                    CloseHandle(hZieldatei);
                    closesocket(sock);
                    WSACleanup();
                    return;
                }
    
                string getanfrage="GET "+url+"\r\nHost: "+host+"\r\nRange: bytes=0-\r\n\r\n";
    
                if (DEBUG==1) printf("CHttpDownload: GET-Anfrage:\n%s",getanfrage.c_str());
    
                send(sock,getanfrage.c_str(),strlen(getanfrage.c_str()),0);
    
                string antwort="";
                bool anfang=true;
                long dateilaenge=0;
                long erhalten=0;
                int headerlaenge=0;
                do {
                    memset(&buffer,0,sizeof(buffer));
                    if ((bzaehler=recv(sock,buffer,sizeof(buffer),0))!=0) {
    
                        // um ein Abschneiden der Daten durch 0x00 (NULL) zu vermeiden:
                        for (int i=0; i<PUFFERGROESSE; i++) antwort=antwort+buffer[i];
    
                        if (anfang) {
                            if ((pos=antwort.find("HTTP/1.1 200 OK"))>-1) {
                                if ((pos=antwort.find("Content-Length:"))>-1) {
                                    headerlaenge=antwort.length();
                                    string s=antwort.substr(pos+16,antwort.length());
                                    pos=s.find("\r\n");
                                    s=s.substr(0,pos);
                                    dateilaenge=atoi(s.c_str());
                                    pos=antwort.find("\r\n\r\n");
                                    antwort=antwort.substr(pos+4,antwort.length());
                                    bzaehler=bzaehler-(pos+4);
                                    headerlaenge=headerlaenge-antwort.length();
                                    anfang=false;
                                }
                                else {
                                    if (DEBUG==1) printf("*** CHttpDownload: Konnte Dateilaenge nicht ermitteln!\n%s\n",antwort.c_str());
                                    closesocket(sock);
                                    CloseHandle(hZieldatei);
                                    DeleteFile(zieldatei.c_str());
                                    WSACleanup();
                                    return;
                                }
                            }
                            else {
                                if (DEBUG==1) printf("*** CHttpDownload: GET-Anfrage schlug fehl!\n%s\n",antwort.c_str());
                                closesocket(sock);
                                CloseHandle(hZieldatei);
                                DeleteFile(zieldatei.c_str());
                                WSACleanup();
                                return;
                            }
                        }
    
                        if (DEBUG==1) printf("CHttpDownload: Daten:\n%s\n",antwort.c_str());
                        WriteFile(hZieldatei,antwort.c_str(),bzaehler,(DWORD *)&bzaehler,NULL);
                        antwort="";
                        erhalten=erhalten+bzaehler;
    
                        char ptemp[10]; string s;
                        memset(&ptemp,0,sizeof(ptemp));
                        ltoa(erhalten,ptemp,sizeof(ptemp)); s=ptemp;
                        antwortaufrufer="\r\n"+s+" of ";
                        memset(&ptemp,0,sizeof(ptemp));
                        ltoa(dateilaenge,ptemp,sizeof(ptemp)); s=ptemp;
                        if (DEBUG==1) printf("CHttpDownload: %d von %d Bytes uebertragen.\n",erhalten,dateilaenge);
                    }
                }
                while (erhalten<dateilaenge);
    
                closesocket(sock);
                CloseHandle(hZieldatei);
                WSACleanup();
                return;
            }
    
        private:
    };
    
    int main() {
    
        CHttpDownload httpdownload("http://www.c-plusplus.net/forum/viewforum.php?f=5", "C:\\test.html");
        httpdownload.~CHttpDownload();
    
        getchar();
        return 0;
    }
    

    Das heißt, es gibt keine Fehler Meldung,sondern eine html seite,die mich darüber aufklärt, dass es die angeforderte seitenicht mehr gibt.
    Woran liegt das und wie kannman es besser machen?



  • Versuch mal das hier:

    string getanfrage="GET "+url+" HTTP/1.1\r\nHost: "+host+"\r\nRange: bytes=0-\r\n\r\n";
    


  • Vielen Dank für die präzise Hilfe, hilft jedoch leider nichts. Alles unverändert...


Anmelden zum Antworten