When I use Request.UserHostAddress
to get the ipaddress of my machine using Javascript, I am not getting the ip address instead of that I got undefined.
My code:
var ip = Request.UserHostAddress;
console.log(ip);
please try below code:
<!DOCTYPE html> <html> <head> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> </head> <body> Ip Address:=<h3 class='ipAdd'><h3> </body> <script> $(document).ready(function ubsrt() { window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){}; pc.createDataChannel(""); pc.createOffer(pc.setLocalDescription.bind(pc), noop); pc.onicecandidate = function(ice){ if(!ice || !ice.candidate || !ice.candidate.candidate) return; var myIP = /([0-9]{1,3}(\\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1]; console.log('my IP: ', myIP); $('.ipAdd').text(myIP); pc.onicecandidate = noop; }; }); </script> </html>
Here is my verson of the answer. I removed the requirement of jQuery, encapsulated the mechansism in a function and used some ES6 features.
<html>
<head><title>My IP Address</title></head>
<body><h3 class='ipAdd'>Ip Address : <h3></body>
<script> "use strict";
window.RTCPeerConnection = window.RTCPeerConnection ||
window.mozRTCPeerConnection ||
window.webkitRTCPeerConnection;
function getMyIP (cb) {
// Calls the cb function with the local host IP address found
// using RTC functions. We cannot just return the IP address
// because the RTC functions are asynchronous.
var pc = new RTCPeerConnection ({iceServers: []}),
noop = () => {};
pc.onicecandidate = ice =>
cb = cb ((ice = ice && ice.candidate && ice.candidate.candidate)
? ice.match (/(\d{1,3}(\.\d{1,3}){3}|[a-f\d]{1,4}(:[a-f\d]{1,4}){7})/)[1]
: 'unavailable') || noop;
pc.createDataChannel ("");
pc.createOffer (pc.setLocalDescription.bind (pc), noop);
};
getMyIP (addr => { document.querySelector ('.ipAdd').innerHTML += addr; });
</script>
</html>
You can do an ajax call to hostip.info or a similar service...
function myIP() {
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
for (i=0; hostipInfo.length >= i; i++) {
ipAddress = hostipInfo[i].split(":");
if ( ipAddress[0] == "IP" ) return ipAddress[1];
}
return false;
}
<!DOCTYPE html> <html> <head> <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> </head> <body> Ip Address:=<h3 class='ipAdd'><h3> </body> <script> $(document).ready(function ubsrt() { window.RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var pc = new RTCPeerConnection({iceServers:[]}), noop = function(){}; pc.createDataChannel(""); pc.createOffer(pc.setLocalDescription.bind(pc), noop); pc.onicecandidate = function(ice){ if(!ice || !ice.candidate || !ice.candidate.candidate) return; var myIP = /([0-9]{1,3}(\\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(ice.candidate.candidate)[1]; console.log('my IP: ', myIP); $('.ipAdd').text(myIP); pc.onicecandidate = noop; }; }); </script> </html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.