Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Delphi - IP Pubblico (Delphi7)
Forum - Delphi - IP Pubblico (Delphi7)

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 16:59
Venerdė, 06/05/2011
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

  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  8.   IdHTTP, StdCtrls;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     IdHTTP1: TIdHTTP;
  13.     Button1: TButton;
  14.     function GetPublicIp: String;
  15.     procedure Button1Click(Sender: TObject);
  16.   private
  17.     { Private declarations }
  18.   public
  19.     { Public declarations }
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.dfm}
  28.  
  29. function TForm1.GetPublicIp: String;
  30. var B, E, I: Integer;
  31.    Str, SStr, EStr, R: String;
  32.    IdHTTP1: TIdHTTP;
  33. begin
  34.  Result := '';
  35.  B := 0;
  36.  E := 0;
  37.  IdHTTP1 := TIdHTTP.Create(nil);
  38.  try
  39.    //Str := IdHTTP1.Get('http://www.whatismyip.com/');
  40.    Str := IdHTTP1.Get('http://www.ip-adress.com/');
  41.  finally
  42.    FreeAndNil(IdHTTP1);
  43.  end;
  44.  if B=0 then begin
  45.    SStr := 'Your IP  Is ';
  46.    B := Pos(SStr, Str);
  47.  end;
  48.  if B=0 then begin
  49.    SStr := 'Your IP  - ';
  50.    B := Pos(SStr, Str);
  51.  end;
  52.  if B=0 then begin
  53.    SStr := 'My IP address: ';
  54.    B := Pos(SStr, Str);
  55.  end;
  56.  if B=0 then
  57.    raise Exception.Create('Stringa non trovata! ('+SStr+')');
  58.  if E=0 then begin
  59.    EStr := 'WhatIsMyIP';
  60.    //E := Pos(EStr, Str);
  61.    E := B+16+Length(SStr);
  62.  end;
  63.  if E=0 then
  64.    raise Exception.Create('Stringa non trovata! ('+EStr+')');
  65.  Result := '';
  66.  R := Copy(Str, B+Length(SStr), E-B-Length(SStr)-1);
  67.  for I := 1 to Length(R) do
  68.    if Pos(R[I], '0123456789.')>0 then
  69.      Result := Result+R[I];
  70. end;
  71.  
  72. procedure TForm1.Button1Click(Sender: TObject);
  73. begin
  74.   ShowMessage(GetPublicIp);
  75. end;
  76.  
  77. end.


é il codice sbagliato o č il sito usato? li ho utilizzati entrambi ma da sempre lo stesso errore.

PM Quote
Avatar
smanettone83 (Normal User)
Pro


Messaggi: 124
Iscritto: 20/10/2010

Segnala al moderatore
Postato alle 17:04
Venerdė, 06/05/2011

PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 21:47
Venerdė, 06/05/2011
Ho fatto cosi ma non mi comapre l'IP:
Codice sorgente - presumibilmente Delphi

  1. function TForm1.GetPublicIp: String;
  2.     var
  3.       IP: String;
  4.       begin
  5.       IdHttp1.Request.Host:= 'http://www.whatismyip.com';
  6.       IP := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp');
  7. end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. begin
  11.   ShowMessage(GetPublicIp);
  12. end;


PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 22:12
Venerdė, 06/05/2011
Testo quotato

Postato originariamente da camaleonteplus:

Ho fatto cosi ma non mi comapre l'IP:
Codice sorgente - presumibilmente Delphi

  1. function TForm1.GetPublicIp: String;
  2.     var
  3.       IP: String;
  4.       begin
  5.       IdHttp1.Request.Host:= 'http://www.whatismyip.com';
  6.       IP := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp');
  7. end;
  8.  
  9. procedure TForm1.Button1Click(Sender: TObject);
  10. begin
  11.   ShowMessage(GetPublicIp);
  12. 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

  1. function TForm1.GetPublicIp: String;
  2.     var
  3.       begin
  4.       IdHttp1.Request.Host:= 'http://www.whatismyip.com';
  5.       result := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp');
  6. end;
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. begin
  10.   ShowMessage(GetPublicIp);
  11. end;


forse cosė funziona :)
G.


Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 22:18
Venerdė, 06/05/2011
con questo codice mi da questo errore:
Codice sorgente - presumibilmente Delphi

  1. Function GetMyIP: String;
  2. Var oHttp: TIdHTTP;
  3.     sStream: TStringgrid ;
  4.     nPos: Integer;
  5. begin
  6.   sStream := TStringgrid.Create;
  7.   oHttp := TIdHTTP.Create(Nil);
  8.   Result := 'Error';
  9.   Screen.Cursor := crHourGlass;
  10.   try
  11.     Try
  12.       oHttp.Get('http://www.ilmioip.it', sStream);  // tutta la pagina č nello stream
  13.       nPos :=Pos('var IP_CLIENT =', sStream.DataString);
  14.       if nPos>0 then
  15.         Result:= copy(sStream.DataString,npos+17,14);
  16.     Except
  17.       ShowMessage('gestioneerrori');
  18.     End;
  19.   Finally
  20.     freeandnil(sStream);
  21.     freeandnil(oHttp);
  22.     Screen.Cursor := crDefault;
  23.   end;
  24. end;


[Error] Unit1.pas(36): Not enough actual parameters
l'errore č su questa riga
sStream := TStringgrid.Create;

PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 22:30
Venerdė, 06/05/2011
Testo quotato

Postato originariamente da camaleonteplus:

con questo codice mi da questo errore:
Codice sorgente - presumibilmente Delphi

  1. Function GetMyIP: String;
  2. Var oHttp: TIdHTTP;
  3.     sStream: TStringgrid ;
  4.     nPos: Integer;
  5. begin
  6.   sStream := TStringgrid.Create;
  7.   oHttp := TIdHTTP.Create(Nil);
  8.   Result := 'Error';
  9.   Screen.Cursor := crHourGlass;
  10.   try
  11.     Try
  12.       oHttp.Get('http://www.ilmioip.it', sStream);  // tutta la pagina č nello stream
  13.       nPos :=Pos('var IP_CLIENT =', sStream.DataString);
  14.       if nPos>0 then
  15.         Result:= copy(sStream.DataString,npos+17,14);
  16.     Except
  17.       ShowMessage('gestioneerrori');
  18.     End;
  19.   Finally
  20.     freeandnil(sStream);
  21.     freeandnil(oHttp);
  22.     Screen.Cursor := crDefault;
  23.   end;
  24. 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

  1. Function GetMyIP: String;
  2.     Var oHttp: TIdHTTP;
  3.         sStream: TStringStream ;
  4.         nPos: Integer;
  5.     begin
  6.       sStream := TStringStream.Create;
  7.       oHttp := TIdHTTP.Create(Nil);
  8.       Result := 'Error';
  9.       Screen.Cursor := crHourGlass;
  10.       try
  11.         Try
  12.           oHttp.Get('http://www.ilmioip.it', sStream);  // tutta la pagina č nello stream
  13.           nPos :=Pos('var IP_CLIENT =', sStream.DataString);
  14.           if nPos>0 then
  15.             Result:= copy(sStream.DataString,npos+17,14);
  16.         Except
  17.           ShowMessage('gestioneerrori');
  18.         End;
  19.       Finally
  20.         freeandnil(sStream);
  21.         freeandnil(oHttp);
  22.         Screen.Cursor := crDefault;
  23.       end;
  24.     end;



Ibis redibis non morieris in bello
PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 6:47
Sabato, 07/05/2011
con questo codice:
Codice sorgente - presumibilmente Delphi

  1. function TForm1.GetPublicIp: String;
  2.     var
  3.       begin
  4.       IdHttp1.Request.Host:= 'http://www.whatismyip.com';
  5.       result := IdHttp1.Get('http://www.whatismyip.com/automation/n09230945.asp');
  6. end;


mi da questo errore:
HTTP/1.1 503 Service Unavailable  

PM Quote
Avatar
camaleonteplus (Normal User)
Pro


Messaggi: 95
Iscritto: 05/03/2009

Segnala al moderatore
Postato alle 6:52
Sabato, 07/05/2011
con questo codice:
Codice sorgente - presumibilmente Delphi

  1. Function GetMyIP: String;
  2.         Var oHttp: TIdHTTP;
  3.             sStream: TStringStream ;
  4.             nPos: Integer;
  5.         begin
  6.           sStream := TStringStream.Create;
  7.           oHttp := TIdHTTP.Create(Nil);
  8.           Result := 'Error';
  9.           Screen.Cursor := crHourGlass;
  10.           try
  11.             Try
  12.               oHttp.Get('http://www.ilmioip.it', sStream);  // tutta la pagina č nello stream
  13.               nPos :=Pos('var IP_CLIENT =', sStream.DataString);
  14.               if nPos>0 then
  15.                 Result:= copy(sStream.DataString,npos+17,14);
  16.             Except
  17.               ShowMessage('gestioneerrori');
  18.             End;
  19.           Finally
  20.             freeandnil(sStream);
  21.             freeandnil(oHttp);
  22.             Screen.Cursor := crDefault;
  23.           end;
  24.         end;


da questo errore:
[Error] Unit1.pas(35): Not enough actual parameters in questa riga:
Codice sorgente - presumibilmente Plain Text

  1. sStream := TStringStream.Create;


PM Quote
Avatar
Goblin (Member)
Expert


Messaggi: 375
Iscritto: 02/02/2011

Segnala al moderatore
Postato alle 12:05
Sabato, 07/05/2011
Testo quotato

Postato originariamente da camaleonteplus:

da questo errore:
[Error] Unit1.pas(35): Not enough actual parameters in questa riga:
Codice sorgente - presumibilmente Plain Text

  1. 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
PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo