1,317
社区成员
发帖
与我相关
我的任务
分享
//---------------------------------------------------------------------------
#ifndef SimpleHTTPProtocolH
#define SimpleHTTPProtocolH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>
//---------------------------------------------------------------------------
class TSimpleHTTP : public TIdTCPClientCustom
{
private:
IIdTextEncoding * FTextEncoding;
bool __fastcall SendHeader(String AURL, int APostDataLength = 0);
int __fastcall ReceiveResponse(void);
protected:
void __fastcall DoOnConnected(void);
public:
String __fastcall Get(String AURL);
String __fastcall Post(String AURL, String AData);
bool __fastcall Download(String AURL, TStream * AStream);
__fastcall TSimpleHTTP(TComponent * AOwner, IIdTextEncoding * AEncoding = NULL);
__fastcall ~TSimpleHTTP(void);
};
#endif
SimpleHTTPProtocol.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "SimpleHTTPProtocol.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma argsused
//---------------------------------------------------------------------------
__fastcall TSimpleHTTP::TSimpleHTTP(TComponent* AOwner,
IIdTextEncoding * AEncoding) : TIdTCPClientCustom(AOwner)
{
if (AEncoding)
FTextEncoding = AEncoding;
else FTextEncoding = IndyTextEncoding_OSDefault();
}
//---------------------------------------------------------------------------
__fastcall TSimpleHTTP::~TSimpleHTTP(void)
{
if (Connected())
{
IOHandler->CloseGracefully();
Disconnect(false);
}
}
//---------------------------------------------------------------------------
bool __fastcall TSimpleHTTP::SendHeader(String AURL, int APostDataLength)
{
bool LResult = false;
String LHost, S;
Word LPort = 80;
if (AURL.Length() < 8) return LResult;
if (AURL.UpperCase().Pos("HTTP://") != 1) return LResult;
AURL.Delete(1, 7);
if (AURL.Pos("/"))
{
LHost = AURL.SubString(1, AURL.Pos("/") - 1);
AURL.Delete(1, AURL.Pos("/") - 1);
}
else
{
LHost = AURL;
AURL = "/";
}
int LLength = LHost.Pos(":");
if (LLength)
{
S = LHost;
S.Delete(1, LLength);
LPort = S.ToIntDef(80);
LHost.SetLength(LLength - 1);
}
if (!SameText(Host, LHost) || Port != LPort)
{
if (Connected()) Disconnect();
Host = LHost;
Port = LPort;
}
if (!Connected()) Connect();
if (Connected())
{
S = (APostDataLength ? "POST" : "GET");
IOHandler->WriteLn(S + " " + AURL + " HTTP/1.1");
IOHandler->WriteLn("Connection: Keep-Alive");
IOHandler->WriteLn("Content-Type: application/x-www-form-urlencoded");
if (APostDataLength)
IOHandler->WriteLn("Content-Length: " + IntToStr(APostDataLength));
S = LHost;
if (LPort != 80)
S += ":" + IntToStr(LPort);
IOHandler->WriteLn("Host: " + S);
IOHandler->WriteLn("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
IOHandler->WriteLn("Accept-Encoding: identity");
IOHandler->WriteLn("User-Agent: Mozilla/3.0 (compatible; Indy Library)");
IOHandler->WriteLn();
LResult = true;
}
return LResult;
}
//---------------------------------------------------------------------------
int __fastcall TSimpleHTTP::ReceiveResponse(void)
{
int LResult = 0;
bool HttpOK = false;
String S;
do
{
S = IOHandler->ReadLn();
if (SameText(S.SubString(1, 9), "HTTP/1.1 "))
{
HttpOK = S.Pos(" 200 OK");
}
else if (SameText(S.SubString(1, 15), "Content-Length:"))
{
S.Delete(1, S.Pos(" "));
LResult = S.ToIntDef(0);
}
}
while (S.Length());
if (!HttpOK) LResult = 0;
return LResult;
}
//---------------------------------------------------------------------------
void __fastcall TSimpleHTTP::DoOnConnected(void)
{
IOHandler->DefStringEncoding = FTextEncoding;
TIdTCPClientCustom::DoOnConnected();
}
//---------------------------------------------------------------------------
String __fastcall TSimpleHTTP::Get(String AURL)
{
String LResult;
if (SendHeader(AURL))
{
int LLength = ReceiveResponse();
if (LLength)
LResult = IOHandler->ReadString(LLength);
}
return LResult;
}
//---------------------------------------------------------------------------
String __fastcall TSimpleHTTP::Post(String AURL, String AData)
{
String LResult;
TIdBytes LBytes = FTextEncoding->GetBytes(AData);
if (SendHeader(AURL, LBytes.Length))
{
IOHandler->Write(LBytes, LBytes.Length);
int LLength = ReceiveResponse();
if (LLength)
LResult = IOHandler->ReadString(LLength);
}
return LResult;
}
//---------------------------------------------------------------------------
bool __fastcall TSimpleHTTP::Download(String AURL, TStream * AStream)
{
bool LResult = false;
AStream->Size = 0;
if (SendHeader(AURL))
{
int LLength = ReceiveResponse();
if (LLength)
{
IOHandler->ReadStream(AStream, LLength, false);
AStream->Position = 0;
if (AStream->Size == LLength)
LResult = true;
}
}
return LResult;
}
//---------------------------------------------------------------------------