享受自由与创造的乐趣!
I LOVE THIS GAME
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
13 随笔 :: 0 文章 :: 40 评论 :: 0 引用
[转] 通过外部网站返回得到内网外部IP (C#/VB.NET)
通过外部网站返回得到内网外部IP
C#版
static
string
GetIP()
{
Uri uri
=
new
Uri(
"
http://www.ikaka.com/ip/index.asp
"
);
System.Net.HttpWebRequest req
=
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method
=
"
POST
"
;
req.ContentType
=
"
application/x-www-form-urlencoded
"
;
req.ContentLength
=
0
;
req.CookieContainer
=
new
System.Net.CookieContainer();
req.GetRequestStream().Write(
new
byte
[
0
],
0
,
0
);
System.Net.HttpWebResponse res
=
(System.Net.HttpWebResponse)(req.GetResponse());
StreamReader rs
=
new
StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(
"
GB18030
"
));
string
s
=
rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Match m
=
System.Text.RegularExpressions.Regex.Match(s,
@"
IP:\[(?<IP>[0-9\.]*)\]
"
);
if
(m.Success)
return
m.Groups[
"
IP
"
].Value;
return
string
.Empty;
}
VB.NET 版
Function
GetIP()
As
String
Dim
Cookies
As
Net.CookieContainer
=
Nothing
Dim
httpurl
As
String
=
"
http://www.ikaka.com/ip/index.asp"
Dim
code
As
String
=
"
GB2312"
Dim
Url
As
New
System.Uri(httpurl)
Dim
SomeByte()
As
Byte
=
System.Text.Encoding.ASCII.GetBytes(
""
)
Dim
req
As
Net.HttpWebRequest
=
CType
(Net.WebRequest.Create(Url), Net.HttpWebRequest)
req.Method
=
"
POST"
req.ContentType
=
"
application/x-www-form-urlencoded"
req.ContentLength
=
0
If
Not
(Cookies
Is
Nothing
)
Then
req.CookieContainer
=
Cookies
Else
req.CookieContainer
=
New
Net.CookieContainer
Cookies
=
req.CookieContainer
End
If
req.GetRequestStream.
Write
(SomeByte,
0
, SomeByte.Length)
Dim
res
As
Net.HttpWebResponse
=
CType
(req.GetResponse, Net.HttpWebResponse)
Dim
ReadStream
As
IO.StreamReader
=
New
IO.StreamReader(res.GetResponseStream, System.Text.Encoding.GetEncoding(code))
Dim
strLine
As
String
Dim
getstr
As
String
Do
strLine
=
ReadStream.ReadLine
getstr
&=
strLine
&
vbCrLf
Loop
Until
strLine
Is
Nothing
ReadStream.Close()
req.Abort()
res.Close()
Dim
reg
As
New
System.Text.RegularExpressions.Regex(
"
<span class=""en"">IP:\[(?<ipstr>.*)?\]\s*</span>
"
, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
Dim
m
As
System.Text.RegularExpressions.Match
m
=
reg.Match(getstr)
If
m.Success
Then
Return
m.Groups(
"
ipstr
"
).Value.ToString
End Function
posted on 2005-08-18 23:40
helloworld84
阅读(1716)
评论(0)
编辑
收藏
网摘
所属分类:
VB.NET
C#
刷新评论列表
刷新页面
返回页首
发表评论
昵称:
[登录]
[注册]
主页:
邮箱:
(仅博主可见)
验证码:
看不清,换一个
评论内容:
登录
注册
[使用Ctrl+Enter键快速提交评论]
0
218104
链接:
切换模板
导航:
网站首页
社区
新闻
博问
闪存
网摘
招聘
找找看
Google搜索
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务
相关文章:
最新IT新闻:
Twitter无处不在 魔兽世界Twitter发送器插件发布
Firefox 3.5匆忙推出漏洞多 Mozilla本月将更新
预测:Twitter最可能收购的十家公司
网易澄清:与暴雪合资公司仅提供技术支持
杰克逊悼念仪式或成史上最大规模Web活动
相关链接:
<
2005年8月
>
日
一
二
三
四
五
六
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
公告
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参与的团队
广州.NET俱乐部(0/0)
Ajax&Atlas技术团队(0/0)
随笔分类
(9)
ASP.NET(3)
(rss)
C#(5)
(rss)
VB.NET(1)
(rss)
Windows
(rss)
正则表达式
(rss)
随笔档案
(13)
2005年10月 (2)
2005年9月 (4)
2005年8月 (5)
2005年7月 (2)
相册
MM
.NET
CSDN
MSDN Library
The Code Project
最新评论
1. re: 获取目录大小
兄弟你的代码,给帮助很大,谢谢! 同时给你提个小小的改进意见类变量“static long longDirSize = 0;”定义好像有点多余,把它改成GetDirSize方法的内部定义,“GetDi...
--老三杨
2. re: 省市联动菜单(ASP.NET + XML)
这个文章也不错~~AJAX的
Asp.net用Ajax实现省市联动
--墙头草
3. re: 获取某目录下的所有文件(包括子目录下文件)的数量(C#/VB.NET)
string [] fileInfos= System.IO.Directory.GetFiles("C:\windwos", "*.*"...
--ytryrtyrturturt
4. re: 发现 C# 2005 Beta2 的一个BUG
楼主是超级菜鸟,鉴定完毕
--TTT
5. re: 获取某目录下的所有文件(包括子目录下文件)的数量(C#/VB.NET)
顶~
--冰凝
阅读排行榜
1. 获取某目录下的所有文件(包括子目录下文件)的数量(C#/VB.NET)(3107)
2. [转] 通过外部网站返回得到内网外部IP (C#/VB.NET)(1716)
3. 省市联动菜单(ASP.NET + XML)(1589)
4. 基于 System.Web.Mail 的邮件发送和接收类(1433)
5. 发现 C# 2005 Beta2 的一个BUG(734)
评论排行榜
1. 高兴....收到微软中国寄给我的第4期的《MSDN 开发精选》(10)
2. 发现 C# 2005 Beta2 的一个BUG(8)
3. 怪事, VS.NET 疯掉了, 注释掉的代码居然还执行(5)
4. 省市联动菜单(ASP.NET + XML)(4)
5. JS + WebService 模拟 IE 的自动完成(3)