delphi 如何获取临时公网ip

linlingwei 2009-04-09 09:35:14
delphi 如何获取临时公网ip?
无论是通过路由器还是直接连接宽待的,都能获取.
...全文
654 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ITlover 2009-04-13
  • 打赏
  • 举报
回复
给你一段网摘,我用过,可以的。不知你是否觉得“取巧”。

{
其实说起来,要从本机直接获取公网 IP 是没可能的,必须在访问时,由网络返回 IP,
用户才能知道自己的公网 IP 到底是什么。这段代码也是取了巧的,直接通过公网页
面得到 IP,并且返回给调用者。}


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ComObj;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
function GetIP: string;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
edit1.Text:=getip;
end;


function TForm1.GetIP: string;
var
xml : OleVariant;
r:string;
p1,p2 : Integer;
begin
xml := CreateOleObject('Microsoft.XMLHTTP');
xml.Open('GET','http://www.net.cn/static/customercare/yourIP.asp', False);
// xml.Open('GET','http://www.china.com', False);
xml.Send;
r := xml.responseText;
p1:=Pos('<h2>',r); // 找到 h2 标签
p2:=Pos('</h2>',r); // 找到 h2 结束标签
Result := Copy(r, p1+4, p2-p1-4);
end;


end.
ahjoe 2009-04-12
  • 打赏
  • 举报
回复
http://www.ahjoe.com/count10/ip.asp
linlingwei 2009-04-11
  • 打赏
  • 举报
回复
如何从下面代码中取得ip地址?
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>[VBnet Internet]  Public IP Address SSI Page</title>
</head>

<body>

<script language="javascript">

var ip = '60.216.219.26'

function ipval() {
document.write(ip);
}
这个ip地址是随时都在变的,有时10数字,有时会更多.如何准确的取出ip地址呢?
谢谢,最好用copy的方法.
devhp 2009-04-11
  • 打赏
  • 举报
回复
直接用它獲取

str := WebBrowser1.OleObject.document.parentWindow.ip;


[Quote=引用 11 楼 devhp 的回复:]
引用此單元
http://download.csdn.net/source/1199303

直接showmessage(RasGetIPAddress;);

路由上網未作測試,拔號IP是沒問題的
[/Quote]
devhp 2009-04-11
  • 打赏
  • 举报
回复
引用此單元
http://download.csdn.net/source/1199303

直接showmessage(RasGetIPAddress;);

路由上網未作測試,拔號IP是沒問題的
wzca 2009-04-10
  • 打赏
  • 举报
回复
好象用NAT穿透可以获取外网IP。你可以查查相关的东西。
我也去找找看
duduxia 2009-04-10
  • 打赏
  • 举报
回复
通过网页来获取外网IP.
sanguomi 2009-04-09
  • 打赏
  • 举报
回复
我以前都是从网页上取的
masterjames 2009-04-09
  • 打赏
  • 举报
回复
取本地IP,用SOCKET 发过来就行了。
无条件为你 2009-04-09
  • 打赏
  • 举报
回复
外网IP需要通过网页来获取。我是用ASP+webbrowser来实现的。
lyguo 2009-04-09
  • 打赏
  • 举报
回复
uses 加入winsock单元.

function LocalIP:string;//获得本机的ip地址
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
BEGIN
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
IF phe = nil THEN Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do BEGIN
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
END;
WSACleanup;
END;


如果在内网,则取得的只是内网的IP
Alex-16888 2009-04-09
  • 打赏
  • 举报
回复
同意楼上的
xiaoxiao_8 2009-04-09
  • 打赏
  • 举报
回复
访问:http://www.ip138.com/
查找"您的IP地址是"
genispan 2009-04-09
  • 打赏
  • 举报
回复
取外网IP要连接到外网才可以获取到 一个简单的例子:

function pro_result(str:string):string ;//封装了成一个匹配的处理函数
var
i_0,i_1:Integer;
flag:Boolean;
str_1,temp_result:string;
begin
temp_result:='';
while Pos('查询结果',str)<>0 do
begin
i_1:=Pos('</span>',str);
i_0:=Pos('查询结果',str);
temp_result:=temp_result+Copy(str,i_0,i_1-i_0)+#13#10;
Delete(str,1,Pos('</span>',str));
end;
temp_result:=StringReplace(temp_result,' ','',[rfreplaceall,rfignorecase]);
Result:=temp_result;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
temp,str_1,str_2:string;
i:Integer;
begin
Memo1.Text:=IdHTTP1.Get('http://www.ipseeker.cn');
temp:=Memo1.Text;
i:=Pos('查询结果',temp);
str_1:=Copy(temp,i,254);
str_2:=pro_result(str_1);
Memo2.Text:=str_2;
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧