请教:C#编程如何获取访问者IP地址?

leo0708 2004-10-28 03:46:48
如题,请高手们多多指点.
...全文
1032 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
winnergx 2004-10-28
  • 打赏
  • 举报
回复
Request.UserHostName
kwklover 2004-10-28
  • 打赏
  • 举报
回复
这个问题好呵
Request.ServerVariables["REMOTE_ADDR"].ToString(); 这个在asp时代就是这个用法了,通过服务器变量来获取的,

Request.UserHostAddress是否可行,没试验过,不好说了 ,回去试验一下在来
adam 2004-10-28
  • 打赏
  • 举报
回复
using System.Collections.Specialized;
加上这个引用。
trnbo 2004-10-28
  • 打赏
  • 举报
回复
TcpListener listener=new TcpListener(port);

listener.Start();
Console.WriteLine("正在等待客户端连接");
Console.WriteLine("按 Ctrl+C 退出...");

while (cont)
{
// 接受将阻塞,直到有人连接
Socket socket = listener.AcceptSocket();
Console.WriteLine(socket.RemoteEndPoint.ToString());
先连接,然后再BeginReceive.
adam 2004-10-28
  • 打赏
  • 举报
回复
///---------------------test-----------------------------------
int loop1, loop2;
NameValueCollection coll;

// Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + "<br>" + arr1[loop1] + "<br>");
String[] arr2=coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + "<br>" + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
///------------------------------------------------------------
把所有的信息都可以打印出来, 当然以"REMOTE_ADDR"来取客户的ip address
cs920 2004-10-28
  • 打赏
  • 举报
回复
Request.ServerVariables("HTTP_X_FORWARDED_FOR");
如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,可以这样处理:
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
chinesetree 2004-10-28
  • 打赏
  • 举报
回复
String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
取远程用户IP地址
sjzwinfor 2004-10-28
  • 打赏
  • 举报
回复
Request.UserHostAddress
sjzwinfor 2004-10-28
  • 打赏
  • 举报
回复
Request.UserHostName

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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