我需要在MVC 5中获取本地系统IP地址和浏览器代理(firefox,chorme,ie等)。搜索google Request.ServerVariables [“REMOTE_ADDR”],这在MVC5中不起作用
答案 0 :(得分:3)
获取IP地址使用此代码:
Part * walker = firstPointer;
Part * stalker = NULL;
if(walker == NULL){
cout << "Empty list" << endl;
return;
}
while (walker != NULL){
//stalker = walker; //not here!
if (partNum == walker->partNo){
if(walker == firstPointer){ //first node
//do your stuff, delete walker
...
firstPointer = NULL;
}
else{
stalker->link = walker->link;
//do stuff, delete walker
...
}
return;
}
stalker = walker; //here!
walker = walker->link;
}
cout << "Part Number Not Found..." << endl;
https://stackoverflow.com/a/7348761/4568359
=============================================== =================
获取浏览器信息:
public static string GetIPAddress(HttpRequestBase request)
{
string ip;
try
{
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
{
if (ip.IndexOf(",") > 0)
{
string[] ipRange = ip.Split(',');
int le = ipRange.Length - 1;
ip = ipRange[le];
}
}
else
{
ip = request.UserHostAddress;
}
}
catch { ip = null; }
return ip;
}
答案 1 :(得分:1)
答案 2 :(得分:1)
获取客户端IP地址
var IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAddress))
{
IPAddress = Request.ServerVariables["REMOTE_ADDR"];
}
获取客户端用户代理。
var userAgent = Request.UserAgent;