C ++ builder(不知道如何处理这段代码)

时间:2016-10-02 05:51:24

标签: 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编译它。我得到语法错误和"多个memo1东西"错误。请帮忙。

1 个答案:

答案 0 :(得分:1)

TMemo是Embarcadero可视化UI组件。 TIdHTTPIndy Project的一个组件,它预装在Delphi和C ++ Builder中。

您获得的代码是与UI相关的代码,因此请为其创建UI。在C ++ Builder IDE中,创建一个新的TForm类,在其上放置TMemo并将其命名为Memo1,然后删除TIdHTTP组件并将其命名为{{ 1}},然后使用类似按钮IdHTTP1处理程序来调用HTTP代码,例如:

Unit1.h:

OnClick

Unit1.cpp:

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

#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

话虽如此,使用字符串来比较日期/时间值通常不是一个好主意。此类字符串受区域设置问题的影响。您正在使用依赖于本地计算机当前区域设置的转换功能,而不是HTTP的标准化日期/时间格式。您使用的//--------------------------------------------------------------------------- #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)); } //--------------------------------------------------------------------------- 属性为TIdHTTP个值,其中TDateTime已将HTTP提供的值转换为本地日期/时间的二进制值(基于本地计算机&#39; s当前时区)。您可以按原样比较这些值,而不必担心任何字符串转换,例如:

TIdHTTP

进行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==运算符,还可以使用!=<运算符:

>