GIF mit dem BCB herunterladen



  • Hey @all,

    und zwar habe ich folgendes Problem, ich will mir ein gif mittels dem BCB herunterladen, jedoch schreibt er mir zum Teil lauter müll in die datei, hier mal der Source:

    void __fastcall TForm1::ClientSocket1Connect(TObject *Sender, TCustomWinSocket *Socket) {
       fstream file(this->filename.c_str(), ios::out | ios::binary);
       AnsiString buf;
       buf.cat_sprintf("GET http://%s/%s/%s HTTP/1.1 \n\n", this->server.c_str(),
                                                            this->server_path.c_str(),
                                                            this->filename.c_str()); //GET command for html transfer
    //   buf+=(" nrnr");     //required as end transfer tag
       Application->MessageBoxA(buf.c_str(), "INFORMATION", MB_OK);
    
       // Neues TWinSocketStream mit einem Timeout von ca. 10000 sec.
       TWinSocketStream* wnscktstrmMain = new TWinSocketStream(Socket, 10000);
        if(wnscktstrmMain->Write(buf.c_str(),buf.Length())){ //if write succeeded, read from stream
          int i=1;
          char point[LENGTH]; // Deklariere buffer
          unsigned int file_length = 0;
          unsigned int max_file_length = LENGTH;  // Nur temporärer wert, kann auch anders lauten nur nicht den seleben wert wie file_length, da er sonst nicht downloaded
          memset(point, 0, LENGTH);// Initialisiere buffer
          if(wnscktstrmMain->Read(point,LENGTH)){
             point[LENGTH]='\0';
             StatusBar1->SimpleText="Success";
             buf=point;
    /*         RichEdit1->Text=Memo1->Text+buf;
             Memo1->Text=Memo1->Text+buf;*/
    //         lblCount->Caption=(lblCount->Caption.ToInt()+1); //incrementing line counter
             int j=0;
                bool do_write = false;
             while(file_length <= max_file_length) {
    /*           if(file_length > 19000) {
               AnsiString t;
               t.cat_sprintf("%d == %d", file_length, max_file_length);
               Application->MessageBoxA(t.c_str(), "", MB_OK);
               }*/
    //         while(i>0&&i<200){
    //            lblCount->Caption=(lblCount->Caption.ToInt()+1); //incrementing line counter
                wnscktstrmMain->Read(point,LENGTH);             //reading from stream
                buf=point;
                int p=0;
    //            while(i!=0&&p>=0&&p<10){    //p is the # of lines to be
                while(i!=0&&p>=0&&p<1){    //p is the # of lines to be
    //             Application->MessageBoxA(point, "POINT", MB_OK);
                  string buffer = buf.c_str();
    //              Application->MessageBoxA(buf.c_str(), "LINES", MB_OK);
                  if(do_write) {
                    file_length += buffer.length();
                    file << point;
                  }
                  if(!do_write) {
                    if(buffer == "\r\n")
                      do_write = true;
                    else if(buffer == "\n")
                      do_write = true;
                    else if(buffer == "\r")
                      do_write = true;
                    else if(buffer.length() == 2)
                      do_write = true;
                    else if(buffer.find("GIF") != string::npos) {
                      int pos = buffer.find("GIF");
                      //file << buffer.substr(buffer.find("GIF"));
    //                  for(int i=pos; i<LENGTH; i++)
                      for(int jj=pos; jj<80; jj++) {
                        file << point[jj];
                      }
                      file_length += buffer.substr(buffer.find("GIF")).length();
                      do_write = true;
                    }
    
                    if(buffer.find("Content") != string::npos) {
                      string content = buffer.substr(buffer.find("Content"));
                      content = content.substr(content.find(":")+1);
                      content = content.substr(0, content.find("\n"));
    
                      while(content.find(" ") != string::npos)
                        content.replace(content.find(" "), 1, "");
    //                  clrStr(content);
                      Memo1->Lines->Add("=====================================");
                      Memo1->Lines->Add(content.c_str());
                      Memo1->Lines->Add("=====================================");
                      stringstream s;
                      s << content;
                      s >> max_file_length;
                    }
                  }
                 p++;
               }
    
               if(i!=0){
    //                 Memo1->Text=Memo1->Text+buf;  //add text as normal
                    i++;
                }
             }
    /*         if(i==((20))){
                Memo1->Lines->Add("");
                Memo1->Lines->Add("");
                Memo1->Lines->Add("*** Max Lines Reached! ***");
                Memo1->Lines->Add("");
             }
             Memo1->Lines->Add("----------------------------");*/
          }
          else
             ShowMessage("No reply");
       }
       else{
          ShowMessage("Connection timed out");
    
       }
       delete wnscktstrmMain;                                    //important
    }
    

    Könnte mir da bitte jemand weiterhelfen, und mir sagen was ich falsch mache?
    Vielen Dank im Voraus

    Lg freeze



  • Ist das eine Fingerübung zum Umgang mit Sockets, oder warum benutzt du keine HTTP-Komponente?



  • Ich verwende BCB Personal, soweit ich weiß gibt es da keine HTTP Komponente, und die Indy's habe ich nicht integrieren können. 😞

    Lg _freeze_

    P.S.: Ich habe bei BCB Personal nur den Client-Socket und den Client-Server dabei



  • Ich will es wirklich mit TClientSocket und nicht den Indy-Komponenten machen

    Kann mir bitte jemand helfen, das Projekt soll bis 20 Dezember 2004 fertig werden (Geburtstagsgeschenk) ich hänge wirklich nur mehr bei dieser Sache.

    Vielen herzlichen Dank im Voraus

    Lg _freeze_


Anmelden zum Antworten