享受自由与创造的乐趣!
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
阅读(3138)
评论(0)
编辑
收藏
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
最想要的Entity Framework功能
·
专访Jeffrey Richter:Windows 8是微软的重中之重
·
《福布斯》:谷歌进军硬件产品 难撼动苹果地位
·
美国空军拟最多购买1.8万台iPad 2
·
分析称专利之争让谷歌苹果两败俱伤
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
<
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
公告
昵称:
helloworld84
园龄:
6年6个月
粉丝:
1
关注:
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
最新评论
阅读排行榜
评论排行榜
推荐排行榜