ip138

package net.ipsearch;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class GetIp {
    public static String tt(String ip) throws Exception{
         String charset = "gb2312";
            String local = "";
            String url = "";

            url = "http://www.ip138.com/ips138.asp?ip="+ip+"&action=2";
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            conn.setDoOutput(true);
            InputStream in = u.openStream();
            StringBuffer s = new StringBuffer();
            String rLine = null;
            BufferedReader bReader = new BufferedReader(new InputStreamReader(in,
                    charset));
            while ((rLine = bReader.readLine()) != null) {
                String tmp_rLine = rLine;
                int str_len = tmp_rLine.length();
                if (str_len > 0)
                    s.append(tmp_rLine);
                tmp_rLine = null;
            }
            in.close();
            String content = s.toString();
            
            if (content.length() > 100) {
                int start = content.indexOf("本站主数据:");
                int end = content.indexOf("</li><li>参考数据一:");
                if (start > 0 && end > 0) {
                    String info = content.substring(start, end).trim();
                    if (info.indexOf(":") > 0) {
                        String country = info.substring(info.indexOf(":")).trim();
                        if (country.length() > 0) {
                            String temp = country.substring(1).trim();
                            if (temp.indexOf(" ") > 0)
                                local = temp.substring(0, temp.indexOf(" ")).trim();
                            else
                                local = temp;
                        }
                    }
                }
            }
            System.out.println(local);
            return local;
    }
    
    public static void main(String[] args) throws Exception {
         tt("124.207.188.178");
    }
}

### Lua 和 IP138 的实现方法及相关技术内容 #### 使用 Lua 编写 HTTP 请求以访问 IP138 API Lua 是一种轻量级的脚本语言,常用于嵌入式系统和 Web 开发中。为了与 IP138 提供的 API 交互,可以使用 `luasocket` 或者更现代的库如 `lua-resty-http` 来发送 HTTP 请求并解析返回的结果。 以下是基于 OpenResty 平台中的 `lua-resty-http` 模块的一个简单示例代码: ```lua local http = require "resty.http" local httpc = http.new() -- 设置超时时间(毫秒) httpc:set_timeout(2000) -- 构造请求参数 local res, err = httpc:request_uri("http://api.ip138.com/ip/?ip=your_ip&key=your_api_key", { method = "GET", headers = { ["Content-Type"] = "application/json" } }) if not res then ngx.say("failed to request: ", err) return end -- 输出响应体 ngx.say(res.body) ``` 此代码片段展示了如何通过 GET 方法向 IP138 发起请求,并打印出服务器返回的内容。需要注意的是,这里的 `your_ip` 和 `your_api_key` 需要被替换为实际使用的 IP 地址以及从 IP138 获取的有效 API 密钥[^7]。 #### 将 Lua 路由集成至 Nginx 配置文件 当希望在 Nginy 中运行上述 Lua 脚本时,可以在 Nginx 的配置文件里定义一个新的 URI 映射规则如下所示: ```nginx location /iplookup { default_type 'text/html'; content_by_lua_block { local http = require "resty.http" local httpc = http.new() -- 设置超时时间为2秒钟 httpc:set_timeout(2000) -- 定义目标IP地址和API Key local target_ip = ngx.var.arg_ip or "unknown" local api_key = "your_api_key" -- 向IP138发起请求 local res, err = httpc:request_uri("http://api.ip138.com/ip/", { method = "GET", query = { ip=target_ip, key=api_key }, headers = { ["Content-Type"] = "application/json" } }) if not res then ngx.say("Error fetching data from IP138:", err) return end -- 返回结果给客户端 ngx.say("<pre>", res.body, "</pre>") } } ``` 这段配置允许用户通过浏览器访问 `/iplookup?ip=<target-ip>` 形式的 URL 即可触发对指定 IP 地址的相关信息查询操作[^8]。 --- ### 技术要点总结 - **Lua RESTY HTTP**: 此模块提供了强大的功能来简化 HTTP/HTTPS 客户端的操作流程,非常适合用来构建微服务架构下的网关层逻辑处理单元。 - **Nginx Content By Lua Block**: 利用该指令可以直接嵌套复杂的业务逻辑而无需额外启动独立的应用程序进程,从而提高系统的吞吐能力和稳定性表现。 - **IP138 API 接口调用**: 用户可以根据具体需求选取不同的字段组合形式提交 POST 数据包或者构造 GET 查询串来进行定制化检索体验优化工作。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值