2016-10-02 56 views
-2

對不起,如果這是錯誤的地方問。我有2個代碼給我:C++ builder(不知道該怎麼用這個代碼)

IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Date)); 
Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Expires)); 
Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->LastModified)); 

而且這樣的:

TDateTime dt; 
AnsiString str = DateToStr(dt); 
IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
if(DateToStr(IdHTTP1->Response->Date) != str) 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Date)); 
if(DateToStr(IdHTTP1->Response->Expires) != str) 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Expires)); 
if(DateToStr(IdHTTP1->Response->LastModified) != str) 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->LastModified)); 

不知怎的,它應該返回時,文件被上傳的日期。但我不知道如何讓C++ builder編譯它。我得到語法錯誤和「多個備忘錄的東西」錯誤。請幫忙。

+1

*我得到的語法錯誤*不包括確切的錯誤信息是無用的,因爲是*多memo1東西*。如果你想在這裏獲得幫助,請包括你正在看到的** exact **錯誤信息。它在屏幕的正前方;你絕對沒有理由不把它們包含在你的問題中。如果你不能提供你看到的實際錯誤,爲什麼我們應該努力嘗試和幫助你? –

+0

E2141聲明語法錯誤,然後E2040聲明的3個實例不正確地終止。備忘錄1出現後,不記得我做了什麼。 – Renox92

+0

沒關係。找到解決方案在這裏http://superuser.com/questions/619592/get-modification-time-of-remote-file-over-http-in-bash-script – Renox92

回答

1

TMemo是英巴卡迪諾視覺用戶界面組件。 TIdHTTPIndy Project的一個組件,預裝在Delphi和C++ Builder中。

您提供的代碼是與UI相關的代碼,因此請爲其創建UI。在C++ Builder IDE中,創建一個新的TForm類,在其上放置一個TMemo並將其命名爲Memo1,然後刪除TIdHTTP組件並將其命名爲IdHTTP1,然後使用類似按鈕OnClick的處理程序來調用HTTP代碼,例如:

Unit1.h:

//--------------------------------------------------------------------------- 

#ifndef Unit1H 
#define Unit1H 
//--------------------------------------------------------------------------- 
#include <System.Classes.hpp> 
#include <Vcl.Controls.hpp> 
#include <Vcl.StdCtrls.hpp> 
#include <Vcl.Forms.hpp> 
#include <IdHTTP.hpp> 
//--------------------------------------------------------------------------- 
class TForm1 : public TForm 
{ 
__published: // IDE-managed Components 
    TMemo *Memo1; 
    TButton *Button1; 
    TButton *Button2; 
    TIdHTTP *IdHTTP1; 
    void __fastcall Button1Click(TObject *Sender); 
    void __fastcall Button2Click(TObject *Sender); 
private: // User declarations 
public:  // User declarations 
    __fastcall TForm1(TComponent* Owner); 
}; 
//--------------------------------------------------------------------------- 
extern PACKAGE TForm1 *Form1; 
//--------------------------------------------------------------------------- 
#endif 

Unit1.cpp:

//--------------------------------------------------------------------------- 

#include <vcl.h> 
#pragma hdrstop 

#include "Unit1.h" 
#include <System.Sysutils.hpp> 

//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
    : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Date)); 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Expires)); 
    Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->LastModified)); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
    TDateTime dt = ...; // assign some value 
    // see functions such as Date(), Now(), EncodeDateTime(), etc, 
    // or use the TDateTimePicker component... 

    System::String str = DateToStr(dt); 

    IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
    if (DateToStr(IdHTTP1->Response->Date) != str) 
      Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Date)); 
    if (DateToStr(IdHTTP1->Response->Expires) != str) 
      Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->Expires)); 
    if (DateToStr(IdHTTP1->Response->LastModified) != str) 
      Memo1->Lines->Add(DateTimeToStr(IdHTTP1->Response->LastModified)); 
} 
//--------------------------------------------------------------------------- 

話雖這麼說,使用字符串比較日期/時間值是不是一個好的IDE一般來說。這些字符串受到語言環境問題的影響。您正在使用依賴於本地計算機當前語言環境的轉換函數,而不是HTTP的標準日期/時間格式。您使用的TIdHTTP屬性值爲TDateTime值,其中TIdHTTP已經將本地日期/時間(基於本地計算機當前時區)的HTTP提供的值轉換爲二進制值。您可以比較這些值,是不用擔心任何字符串轉換,如:

void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
    TDateTime dt = ...; // assign some value 
    // see functions such as Date(), Now(), EncodeDateTime(), etc, 
    // or use the TDateTimePicker component... 

    IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
    if (IdHTTP1->Response->Date != dt) 
    { 
      //... 
    } 
    if (IdHTTP1->Response->Expires != dt) 
    { 
      //... 
    } 
    if (IdHTTP1->Response->LastModified != dt) 
    { 
      //... 
    } 
} 

TDateTime比較比字符串比較更加準確和可靠。而你不侷限於僅僅==!=運營商,您可以使用<>運營商以及:

void __fastcall TForm1::Button2Click(TObject *Sender) 
{ 
    TDateTime dt = Date(); 

    IdHTTP1->Head("http://dsrt.dyndns.org/files/MAIN.zip"); 
    if (IdHTTP1->Response->Date < dt) 
    { 
      // older than today... 
    } 
    if (IdHTTP1->Response->Expires < dt) 
    { 
      // expired prior to today... 
    } 
    if (IdHTTP1->Response->LastModified < dt) 
    { 
      // last modified prior to today... 
    } 
}