If you would like to know your ip address location based on the user ip address. here is best option you can go with.
I have created a visualforce page and an apex controller that makes a callout to external application with the user logged in ip address and will get back the response and displays in vf page.
Add the endpoint url to Remote Site Settings
Vf page:
Apex Class
I have created a visualforce page and an apex controller that makes a callout to external application with the user logged in ip address and will get back the response and displays in vf page.
Add the endpoint url to Remote Site Settings
http://www.telize.com
Vf page:
<apex:page controller="FindIpAddress" action="{!getipaddress}"> Country:{!country}<br/> Isp:{!isp}<br/> IP:{!ip}<br/> Latitude:{!latitude}<br/> Longitude{!longitude}<br/> TimeZone: {!timezone}<br/> </apex:page>
Apex Class
public class FindIpAddress { public String country {get;set;} public String isp {get;set;} public String ip {get;set;} public integer latitude {get;set;} public integer longitude {get;set;} public String timezone {get;set;} public void getipaddress() { string ReturnValue = ''; ReturnValue = ApexPages.currentPage().getHeaders().get('True-Client-IP'); if (ReturnValue == '' || ReturnValue == null) { ReturnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP'); } if (ReturnValue == '' || ReturnValue == null) { ReturnValue = ApexPages.currentPage().getHeaders().get('X-Forwarded-For'); } Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('http://www.telize.com/geoip/'+ReturnValue); req.setMethod('GET'); HttpResponse res; try{ res = h.send(req); Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(res.getbody()); country =(String)m.get('country'); isp=(String)m.get('isp'); ip=(String)m.get('ip'); latitude=(Integer)m.get('latitude'); longitude =(Integer)m.get('longitude'); timezone =(String)m.get('timezone'); }catch(Exception e){ } } }
I copied and Pasted the code but not working
ReplyDeleteHi Karthik,
ReplyDeleteSorry for not mentioning to add remote site settings in the blog.
Add Remote Site Settings. That might help you.
nice article but what do you recommend is it nice to use https instead of http
ReplyDeleteThis is not working. can you please help?
ReplyDelete