Obtain the current geographic location using the IP address

Source: Internet
Author: User

1. interface method:

This method is relatively stable and provides relatively stable data. There are many interfaces to provide. You can refer

Http://www.hujuntao.com/api/the-ip-address-api-and-call-the-interface-method.html

I will not explain them one by one. I will only explain the interfaces provided by Sina:

Sina interface address: http://int.dpool.sina.com.cn/iplookup/iplookup.php

There are two parameters: format and ip

1) IP Address: When the ip address value is not input, the default ip address is the local IP address. You can also specify a specific ip address;

2) format: the data format returned to the client is js and json:

A. Return js format:

Eg: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = js & ip = 115.29.28.108

Return result: var remote_ip_info = {"ret": 1, "start": "115.28.0.0", "end": "115.29.0000255", "country": "\ u4e2d \ u56fd ", "province": "\ u5317 \ u4eac", "city": "\ u5317 \ u4eac", "district": "", "isp": "\ u7535 \ u4fe1 ", "type": "\ u673a \ u623f", "desc": "\ u4e2d \ u56fd \ u4e07 \ u7f51 \ u673a \ u623f \ u7535 \ u4fe1 "};

The returned result is a js variable whose value is in json format. You can transcode Unicode characters to Chinese characters:

Var remote_ip_info = {"ret": 1, "start": "115.28.0.0", "end": "115.29.0000255", "country": "China", "province ": "Beijing", "city": "Beijing", "district": "", "isp": "Telecom", "type": "data center", "desc ": "China hichina data center Telecom "};

B. Return json format:

Eg: http://int.dpool.sina.com.cn/iplookup/iplookup.php? Format = json & ip = 115.29.28.108

Returned results:

{"Ret": 1, "start": "115.28.0.0", "end": "115.29.0000255", "country": "\ u4e2d \ u56fd", "province ": "\ u5317 \ u4eac", "city": "\ u5317 \ u4eac", "district": "", "isp": "\ u7535 \ u4fe1", "type ": "\ u673a \ u623f", "desc": "\ u4e2d \ u56fd \ u4e07 \ u7f51 \ u673a \ u623f \ u7535 \ u4fe1 "}

The returned result is a string in json format. You can transcode Unicode characters to Chinese characters:

{"Ret": 1, "start": "115.28.0.0", "end": "115.29.0000255", "country": "China", "province": "Beijing ", "city": "Beijing", "district": "", "isp": "Telecom", "type": "data center", "desc ": "China hichina data center Telecom "}

Sample Code:

Runat = CellPadding = ForeColor = GridLines = AutoGenerateColumns => <AlternatingRowStyle BackColor =/> <EditRowStyle BackColor =/> <FooterStyle BackColor = Font-Bold = ForeColor =/> <HeaderStyle BackColor = font-Bold = ForeColor =/> <PagerStyle BackColor = ForeColor = HorizontalAlign =/> <RowStyle BackColor =/> <SelectedRowStyle BackColor = Font-Bold = ForeColor =/> <SortedAscendingCellStyle BackColor = /> <SortedAscendingHeaderStyle BackColor =/> <SortedDescendingCellStyle BackColor =/> <SortedDescendingHeaderStyle BackColor =/> <Columns> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: TemplateField> <asp: templateField HeaderText => <ItemTemplate> <div style => <% # Eval () %> </div> </ItemTemplate> </asp: templateField> </Columns> </asp: GridView> </div>View Code ips = List <> list = List <IPDetail> (ips! = & Ips. Count> (ip rs =, result = (! Int32.TryParse (result, = (obj! = Ipd = obj [= obj [= obj [= obj [= obj [= obj [= obj [= obj [=View Code ConvertUnicode2Chinese (= Regex (reg. Replace (result, () Convert. ToInt32 (m. Groups []. Value,View Code PostDataToUrl (System. net. httpWebRequest request = (System. net. httpWebRequest) System. net. httpWebRequest. create (==== (System. IO. memoryStream MS = (I =; I <data. count; I ++ = (I >== (System. IO. stream requestStream = (System. net. httpWebResponse response = (System. IO. stream stream = (System. IO. streamReader reader = result =View Code

Running effect:

2. directly request the webpage:

This method is not recommended. It is very unstable to obtain html information from the webpage. Once other people's webpage templates or styles change, their code will also change, which is a headache, generally, this method is not recommended. It is best to use APIs provided by others to find interfaces. The following provides an address for interested people to study:

Http://www.jb51.net/article/33540.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.