camaleonteplus (Normal User)
Pro
Messaggi: 95
Iscritto: 05/03/2009
|
So come si ricava l'IP Locale usando un componente Indy adesso sto cercando di ricavare quello publico usando questo codice trovato in rete, non da errori di compilazioni ma quando clicco sul bottone mi da questo errore:
HTTP/1.1 403 Forbidden
Codice sorgente - presumibilmente Delphi |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; Button1: TButton; function GetPublicIp: String; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function TForm1.GetPublicIp: String; var B, E, I: Integer; Str, SStr, EStr, R: String; IdHTTP1: TIdHTTP; begin Result := ''; B := 0; E := 0; IdHTTP1 := TIdHTTP.Create(nil); try //Str := IdHTTP1.Get('http://www.whatismyip.com/'); Str := IdHTTP1.Get('http://www.ip-adress.com/'); finally FreeAndNil(IdHTTP1); end; if B=0 then begin SStr := 'Your IP Is '; B := Pos(SStr, Str); end; if B=0 then begin SStr := 'Your IP - '; B := Pos(SStr, Str); end; if B=0 then begin SStr := 'My IP address: '; B := Pos(SStr, Str); end; if B=0 then raise Exception.Create('Stringa non trovata! ('+SStr+')'); if E=0 then begin EStr := 'WhatIsMyIP'; //E := Pos(EStr, Str); E := B+16+Length(SStr); end; if E=0 then raise Exception.Create('Stringa non trovata! ('+EStr+')'); Result := ''; R := Copy(Str, B+Length(SStr), E-B-Length(SStr)-1); for I := 1 to Length(R) do if Pos(R[I], '0123456789.')>0 then Result := Result+R[I]; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPublicIp); end; end.
|
é il codice sbagliato o č il sito usato? li ho utilizzati entrambi ma da sempre lo stesso errore.
|
|
smanettone83 (Normal User)
Pro
Messaggi: 124
Iscritto: 20/10/2010
|
|
|
camaleonteplus (Normal User)
Pro
Messaggi: 95
Iscritto: 05/03/2009
|
Ho fatto cosi ma non mi comapre l'IP:
Codice sorgente - presumibilmente Delphi |
function TForm1.GetPublicIp: String; var IP: String; begin IdHttp1.Request.Host:= 'http://www.whatismyip.com'; IP := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp'); end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPublicIp); end;
|
|
|
Goblin (Member)
Expert
Messaggi: 375
Iscritto: 02/02/2011
|
Postato originariamente da camaleonteplus:
Ho fatto cosi ma non mi comapre l'IP:
Codice sorgente - presumibilmente Delphi |
function TForm1.GetPublicIp: String; var IP: String; begin IdHttp1.Request.Host:= 'http://www.whatismyip.com'; IP := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp'); end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPublicIp); end;
|
|
sbagli candeggio .. piccola svista ?
stai chiamando una funzione dunque hai un ritorno, e dove l'assegni il ritorno ? ad una variabile locale dunque basta una piccola modifica, non mi metto a scrivere il tutto secondo i sacri crismi della programmazione, spero che tu invece lo faccia
Codice sorgente - presumibilmente Delphi |
function TForm1.GetPublicIp: String; var begin IdHttp1.Request.Host:= 'http://www.whatismyip.com'; result := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp'); end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(GetPublicIp); end;
|
forse cosė funziona
G.
Ibis redibis non morieris in bello |
|
camaleonteplus (Normal User)
Pro
Messaggi: 95
Iscritto: 05/03/2009
|
con questo codice mi da questo errore:
Codice sorgente - presumibilmente Delphi |
Function GetMyIP: String; Var oHttp: TIdHTTP; sStream: TStringgrid ; nPos: Integer; begin sStream := TStringgrid.Create; oHttp := TIdHTTP.Create(Nil); Result := 'Error'; Screen.Cursor := crHourGlass; try Try oHttp.Get('http://www.ilmioip.it', sStream); // tutta la pagina č nello stream nPos :=Pos('var IP_CLIENT =', sStream.DataString); if nPos>0 then Result:= copy(sStream.DataString,npos+17,14); Except ShowMessage('gestioneerrori'); End; Finally freeandnil(sStream); freeandnil(oHttp); Screen.Cursor := crDefault; end; end;
|
[Error] Unit1.pas(36): Not enough actual parameters
l'errore č su questa riga
sStream := TStringgrid.Create;
|
|
Goblin (Member)
Expert
Messaggi: 375
Iscritto: 02/02/2011
|
Postato originariamente da camaleonteplus:
con questo codice mi da questo errore:
Codice sorgente - presumibilmente Delphi |
Function GetMyIP: String; Var oHttp: TIdHTTP; sStream: TStringgrid ; nPos: Integer; begin sStream := TStringgrid.Create; oHttp := TIdHTTP.Create(Nil); Result := 'Error'; Screen.Cursor := crHourGlass; try Try oHttp.Get('http://www.ilmioip.it', sStream); // tutta la pagina č nello stream nPos :=Pos('var IP_CLIENT =', sStream.DataString); if nPos>0 then Result:= copy(sStream.DataString,npos+17,14); Except ShowMessage('gestioneerrori'); End; Finally freeandnil(sStream); freeandnil(oHttp); Screen.Cursor := crDefault; end; end;
|
[Error] Unit1.pas(36): Not enough actual parameters
l'errore č su questa riga
sStream := TStringgrid.Create; |
sostituire TStringgrid con TStringStream
Codice sorgente - presumibilmente Delphi |
Function GetMyIP: String; Var oHttp: TIdHTTP; sStream: TStringStream ; nPos: Integer; begin sStream := TStringStream.Create; oHttp := TIdHTTP.Create(Nil); Result := 'Error'; Screen.Cursor := crHourGlass; try Try oHttp.Get('http://www.ilmioip.it', sStream); // tutta la pagina č nello stream nPos :=Pos('var IP_CLIENT =', sStream.DataString); if nPos>0 then Result:= copy(sStream.DataString,npos+17,14); Except ShowMessage('gestioneerrori'); End; Finally freeandnil(sStream); freeandnil(oHttp); Screen.Cursor := crDefault; end; end;
|
Ibis redibis non morieris in bello |
|
camaleonteplus (Normal User)
Pro
Messaggi: 95
Iscritto: 05/03/2009
|
con questo codice:
Codice sorgente - presumibilmente Delphi |
function TForm1.GetPublicIp: String; var begin IdHttp1.Request.Host:= 'http://www.whatismyip.com'; result := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp'); end;
|
mi da questo errore:
HTTP/1.1 503 Service Unavailable
|
|
camaleonteplus (Normal User)
Pro
Messaggi: 95
Iscritto: 05/03/2009
|
con questo codice:
Codice sorgente - presumibilmente Delphi |
Function GetMyIP: String; Var oHttp: TIdHTTP; sStream: TStringStream ; nPos: Integer; begin sStream := TStringStream.Create; oHttp := TIdHTTP.Create(Nil); Result := 'Error'; Screen.Cursor := crHourGlass; try Try oHttp.Get('http://www.ilmioip.it', sStream); // tutta la pagina č nello stream nPos :=Pos('var IP_CLIENT =', sStream.DataString); if nPos>0 then Result:= copy(sStream.DataString,npos+17,14); Except ShowMessage('gestioneerrori'); End; Finally freeandnil(sStream); freeandnil(oHttp); Screen.Cursor := crDefault; end; end;
|
da questo errore:
[Error] Unit1.pas(35): Not enough actual parameters in questa riga:
Codice sorgente - presumibilmente Plain Text |
sStream := TStringStream.Create;
|
|
|
Goblin (Member)
Expert
Messaggi: 375
Iscritto: 02/02/2011
|
Postato originariamente da camaleonteplus:
da questo errore:
[Error] Unit1.pas(35): Not enough actual parameters in questa riga:
Codice sorgente - presumibilmente Plain Text |
sStream := TStringStream.Create;
|
|
Ho controllato, con D2010 il codice funziona, invece con D7 la create della classe TStringStream vuole una variabile di tipo stringa, non ho investigato il motivo, cmq problema risolvibile:
var s:String;
....
sStream := TStringStream.Create(s);
...
Se hai tempo e voglia controlla cosa ci fa lo stream con la variabile passata nel create
Ibis redibis non morieris in bello |
|