gpt4 book ai didi

java - 如何在 Blackberry 中通过 www.ipaddresslocation.org 获取 IP 地址

转载 作者:行者123 更新时间:2023-11-29 06:05:59 25 4
gpt4 key购买 nike

我是 Blackberry 编程的新手。我正在尝试通过 www.ipaddressslocation.org 获取 IP 地址、县、市、州/省

我的来源:

String url = "http://www.ipaddresslocation.org/my-ip-address.php";
// HTTP connection
HttpConnection httpConnection = null;
// Stream connection
StreamConnection streamConnection = (StreamConnection) Connector
.open(url);
httpConnection = (HttpConnection) streamConnection;
int code = httpConnection.getResponseCode();
String strContent = "";
// Check response code
if (code == HttpConnection.HTTP_OK) {
InputStream is = httpConnection.openInputStream();
int length = (int) httpConnection.getLength();
// Content is empty
if (length != -1) {
byte incomingData[] = new byte[length];
is.read(incomingData);
strContent = new String(incomingData);
// Write content
} else {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
strContent = new String(bytestream.toByteArray());
bytestream.close();
}
}

完成此方法后,我得到了 strContent 返回值。详细内容:

\ndocument.write('<table><tr><td>Hello, visitor from: <strong> Takatsuki, Japan</strong>');document.write('<img src=\'http://www.ipaddresslocation.org/flags/jp.png\'></td></tr>');document.write('<tr><td>Your Country Code: <b>JP</b></td></tr>');document.write('<tr><td>Your IP State: <b>Osaka</b></td></tr>');document.write('<tr><td>Your IP Address: <b>202.213.220.66</b></td></tr>');document.write('<tr><td>Your Hostname: <b>ns.isb.co.jp</b></td></tr>');document.write('<tr><td>Your ISP: <b>So-net Entertainment Corporation</b></td></tr>');document.write('<tr><td>Your Organization: <b>ISB CORP.</b></td></tr></table>');

如何从上面的内容中获取IP、国家、州、城市?

谢谢和最好的问候!

最佳答案

由于没有标准格式,即 JSON 或 XML,您必须自己解析响应。要编写您的抓取工具,只需仔细检查响应格式并设计您的算法。

例如,如果我们用“;”分割响应,响应如下所示。

document.write('<table><tr><td>Hello, visitor from: <strong> Dhaka, Bangladesh</strong>')
document.write('<img src=\'http://www.ipaddresslocation.org/flags/bd.png\'></td></tr>')
document.write('<tr><td>Your Country Code: <b>BD</b></td></tr>')
document.write('<tr><td>Your IP State: <b>Dhaka</b></td></tr>')
document.write('<tr><td>Your IP Address: <b>116.212.105.42</b></td></tr>')
document.write('<tr><td>Your Hostname: <b>ws9-tetrasoft-dm-ac1-p16.telnet.com.bd</b></td></tr>')
document.write('<tr><td>Your ISP: <b>Telnet Communication Limited</b></td></tr>')
document.write('<tr><td>Your Organization: <b>Telnet Communication Limited</b></td></tr></table>')

然后你可以从每一行中删除“document.write('”和“')”..

那么响应会变成

<table><tr><td>Hello, visitor from: <strong> Dhaka, Bangladesh</strong>
<img src=\'http://www.ipaddresslocation.org/flags/bd.png\'></td></tr>
<tr><td>Your Country Code: <b>BD</b></td></tr>
<tr><td>Your IP State: <b>Dhaka</b></td></tr>
<tr><td>Your IP Address: <b>116.212.105.42</b></td></tr>
<tr><td>Your Hostname: <b>ws9-tetrasoft-dm-ac1-p16.telnet.com.bd</b></td></tr>
<tr><td>Your ISP: <b>Telnet Communication Limited</b></td></tr>
<tr><td>Your Organization: <b>Telnet Communication Limited</b></td></tr></table>

现在您可以使用任何 HTML 解析器或自己解析剩余部分...

只是解决问题...

例如要获取IP地址,需要解析第5行..

删除索引0到<b>的所有字符+ 3....这将从该行中删除部分“<tr><td>Your IP Address: <b>”......然后从 </b> 的索引中删除字符到最后...这将从该行中删除“”...您将得到“116.212.105.42”作为剩余...

关于java - 如何在 Blackberry 中通过 www.ipaddresslocation.org 获取 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com