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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TStringList *st = new TStringList;
	String as = "http://delphi.ktop.com.tw/userlogin.php?action=post";
	st->Add("venderName=aftcast&venderPwd=8886&Submit=%E7%A2%BA%E5%AE%9A");
	TMemoryStream *ms = new TMemoryStream;
	this->IdHTTP1->Post(as,st,ms);
	ms->Position = 0;
	_mmo1->Lines->LoadFromStream(ms);
	delete ms;
	delete st;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdHTTP1Redirect(TObject *Sender, UnicodeString &dest, int &NumRedirect,
          bool &Handled, UnicodeString &VMethod)
{
	ShowMessage("abc");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{

	String as = "http://delphi.ktop.com.tw/";
	String us;

	TStringStream *ss = new TStringStream("",TEncoding::UTF8, false);
	this->IdHTTP1->Get(as,ss);
	ss->Position = 0;
	//_mmo1->Lines->LoadFromStream(ms);
	//UTF8ToUnicodeString(ss->DataString);
	//ss->SaveToFile("abc");
	_mmo1->Lines->Text = ss->DataString;
	//->LoadFromStream(ss);
	//String us;
	//RawByteString rb = ss->DataString;
	//uf = ss->DataString;
/*
	int len = ss->Size;
	char *buf = new char[len+1];
	buf[len] = 0;

	ss->Read(buf,len);

	us = UTF8ToString(buf);
	//ss->Encoding-> Convert() >EncodingName;
	//us = UTF8ToString(rb);
	_mmo1->Lines->Add(us);
	//_mmo1->Lines->Add(ss->DataString);
	//delete ms;
*/
	delete ss;
	//delete [] buf;

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
	String as = "http://delphi.ktop.com.tw/";
	String us;
	TMemoryStream *ms = new TMemoryStream;

	this->IdHTTP1->Get(as,ms);
	ms->Position = 0;

//	_mmo1->Lines->LoadFromStream(ms);

	int len = ms->Size;
	char *buf = new char[len+1];
	buf[len] = 0;

	ms->Read(buf,len);

	us = UTF8ToString(buf);

	_mmo1->Lines->Add(us);

	delete ms;
	delete [] buf;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
  //這網頁是 big5，所以直接load進 memo中是ok的!
	TStringList *st = new TStringList;
  AnsiString as = "http://www.twse.com.tw/ch/trading/indices/MI_5MINS_HIST/MI_5MINS_HIST.php";
	st->Add("myear=100&mmon=06");
	TMemoryStream *ms = new TMemoryStream;
	this->IdHTTP1->Post(as,st,ms);
	ms->Position = 0;
	_mmo1->Lines->LoadFromStream(ms);
	delete ms;
	delete st;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button5Click(TObject *Sender)
{
	String as = "http://delphi.ktop.com.tw/";
	String us;
	us = this->IdHTTP1->Get(as);
	String us2;//UTF8String utf8;
	//int c = us.CodePage();

	us2 = UTF8ToString((char*)us.data(),100);

	//_mmo1->Lines->LoadFromStream(ms);
	//UTF8ToUnicodeString(ss->DataString);
	//ss->SaveToFile("abc");
	_mmo1->Lines->Text = us2;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
	static bool inter = true;
	if(inter)
		IdHTTP1->Request->UserAgent = L"Mozilla/3.0 (compatible)";
	else
		IdHTTP1->Request->UserAgent = L"Mozilla/3.0 (compatible; Indy Library)";

	inter = !inter;
}
//---------------------------------------------------------------------------

