indyというのを初めて使ったが、楽ちんで読み込める。
とりあえず、NHKラジオ第2の読み込みルーチン
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,wininet,HTTPApp, IdBaseComponent, IdComponent,
IdTCPConnection, IdTCPClient, IdHTTP;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Memo1: TMemo;
IdHTTP1: TIdHTTP;
procedure Button1Click(Sender: TObject);
private
{ Private 宣言 }
public
{ Public 宣言 }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{tring -> Array of Charの変換は、StrPCopy()でできます。
ご質問の例だと
StrPCopy(y,x);
のような感じにすると、xの文字列がyの配列にコピーされます。
逆に、Array of Char -> Stringに変換するには、StrPas()を使います。
x:=StrPas(y);
}
procedure TForm1.Button1Click(Sender: TObject);
var
utf8_1:UTF8String;
// sjis:SJISString;
ansS,outS,testS:AnsiString;
// ansS,outS,testS:String;
outWk:string;
// outWk:ansistring;
tankS :array[1..10] of AnsiString;
// tankS :array[1..10] of String;
tankS2:array[1..10] of string;
wideS:Widestring;
i,ii,iii,iiii,iiiii,j,tankLevel,wkPoint,wkPoint_S,WkPoint_E,WkPoint_E2 :Integer;
wkPoint_x,wkPoint_E3,wkpoint_E4,wkpoint_sB,wkpoint_eB:integer;
wkPoint_stop,wkEnd,ken_line:integer;
wk_Write_cnt:integer;
flag_bo,first_bo:boolean;
wk_address,wk_ans,wk_ans_msg,wk_ans_date,wk_ans_time :string;
wk_ansistr,wk_ansistr_base,wk_ansistr_in:string;
F_Out:textfile;
buf_Out: array[0..10000] of char;
wk_hokui,wk_toukei ,wk_Out,wk_total : string;
wk_hokui_str,wk_hokui_end,wk_toukei_str,Wk_toukei_end,wk_len :integer;
wk_count,wkwk:integer;
TRF:TReplaceFlags;
procedure InterRead(InAddr : Ansistring);
// procedure InterRead(InAddr : string);
var
hSession: HINTERNET;
hService: HINTERNET;
lpBuffer: array[0..1024+1] of AnsiChar;
addr :String;
dwBytesRead: DWORD;
begin
addr := Inaddr;
// hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
hSession := InternetOpen( 'MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned( hSession ) then
begin
hService := InternetOpenUrl( hSession, PChar(addr), nil, 0, 0, 0);
if Assigned( hService ) then
while true do
begin
dwBytesRead := 1024;
InternetReadFile( hService, @lpBuffer, 1024, dwBytesRead );
if dwBytesRead = 0 then break;
lpBuffer[dwBytesRead] := ' ';
lpBuffer[dwBytesRead +1 ] := #0;
ansS := strPas(lpBuffer);
// utf8_1 := strPas(lpBuffer);
// wideS:= strPas(lpBuffer);
// ansS:= UTF8ToString(utf8);
OutS:=UTF8ToString(ansS);
tankS[TankLevel]:=tankS[TankLevel]+OutS;
Memo1.Lines.Add( outS );
end; //while
InternetCloseHandle( hService );
end;
InternetCloseHandle( hSession );
end;
procedure InterRead2(InAddr : Ansistring);
// procedure InterRead(InAddr : string);
var
IdHTTP:TIdHTTP;
addr :String;
begin
Idhttp:= TIdHTTP.Create(nil);
try
ansS :=IdHTTP.Get(InAddr);
finally
IdHTTP.Free;
end;
// OutS:=UTF8ToString(ansS);
OutS:=ansS;
tankS[TankLevel]:=tankS[TankLevel]+OutS;
Memo1.Lines.Add( outS );
end;
begin
for i := 1 to 10 do
tankS[i]:='';
// 県レベルデータ取り込み
assignFile(F_Out,'d:\test\nhk.txt');
system.SetTextBuf(F_Out,Buf_Out);
reWrite(F_Out);
wk_write_cnt:=0;
TankLevel:=1;
wk_ansistr_in:=edit1.Text;
// wk_ansistr:=HTTPEncode(UTF8Encode(WideString(wk_ansistr_in)));
// wk_ansistr:=HTTPEncode(wk_ansistr);
wk_ansistr_base:='http://cgi4.nhk.or.jp/hensei/program/wk.cgi?area=303&tz=all&ch=06&Input=%E5%A4%89%E6%9B%B4%E3%81%99%E3%82%8B';
//+wk_ansistr;
wkpoint_s:=1;
wk_ansistr:=wk_ansistr_base;
InterRead2(wk_ansistr);
Memo1.Lines.Add(tanks[1]);
OutWk:=tanks[1];
wkpoint_s:=Pos('', OutWk);
Delete(OutWk,1,wkPoint_s + 0);
wk_len :=length(OutWk);
wkpoint_e:=Pos('', OutWk);
Delete(OutWk,wkpoint_e,wk_len - wkPoint_e );
if wkpoint_s=0 then
begin
Wk_Out:= 'Err' +#09 + wk_ansistr_in + #09 + OutWk;
writeln(F_Out,Wk_Out);
end
else
begin
wkpoint_sB:=Pos('
', OutWk);
while(wkPoint_sB > 0) do
begin
inc(wk_write_cnt);
wk_ans_time:=copy(OutWk,wkPoint_sB + 21,5);
wkpoint_s:=Pos('&date=',outWk);
wk_ans_date:=copy(OutWk,wkPoint_s + 6,10);
Delete(OutWk,1,wkPoint_s + 16);
wkpoint_s:=Pos('">',outWk);
wkpoint_e:=Pos('',outWk);
wk_ans_msg:=copy(OutWk,wkPoint_s + 2,Wkpoint_E - wkPoint_s - 2 );
wkpoint_x:=Pos('?',wk_ans_msg);
if wkpoint_x > 0 then
wkpoint_x :=1;
Wk_Out:= format('%6u',[wk_write_cnt]) +#09 + wk_ans_date +#09 + wk_ans_time + #09 + wk_ans_msg + #09 ;
writeln(F_Out,Wk_Out);
wkpoint_eB:=Pos('
', OutWk);
Delete(OutWk,1,wkPoint_eB + 6);
wkpoint_sB:=Pos('
', OutWk);
end;
end;
//県名把握
closeFile(F_out);
end;
end.