Tópico antigo, mas como eu tava loko loko procurando, e em tudo quanto é site de busca eu era redirecionado pra cá, tô postando minha GAMBIARRA para que outros possam usar tbm… xD
Eu consegui fazendo uma gambi ao extremo, que se meu chefe (que me ensina algumas coisas de vez em quando…) ver, me demite na hora…
/*
- ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- “License”); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- ====================================================================
-
- This software consists of voluntary contributions made by many
- individuals on behalf of the Apache Software Foundation. For more
- information on the Apache Software Foundation, please see
-
http://www.apache.org/.
-
*/
package UTIL.NET;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
- How to send a request directly using {@link HttpClient}.
-
- @since 4.0
*/
public class LIB_ExternalIP {
public String getExternalIp() {
String received = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpHost target = new HttpHost(“meuip.datahouse.com.br”, 80, “http”);
//HttpHost target = new HttpHost(“meuip.com.br”, 80, “http”);
HttpGet req = new HttpGet("/");
HttpResponse rsp = httpclient.execute(target, req);
HttpEntity entity = rsp.getEntity();
if (entity != null) {
received = EntityUtils.toString(entity);
if (received.contains("<span class=“meuip-home”>meu ip")) {
int a = received.indexOf("<span class=“meuip-home”>meu ip");
int b = received.indexOf("", a);
received = received.substring(a, b);
int f1 = received.indexOf(’.’);
int f0 = received.indexOf(’;’) + 2;
received = received.substring(f0);
System.out.println("External IP Founded: " + received);
}else{
System.out.println(“External IP not Founded. Returning null”);
}
}
} catch (IOException ex) {
Logger.getLogger(LIB_ExternalIP.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
return received;
}
}
Pra esse código funcionar, é preciso as libs do http client, que podem ser encontradas em
http://hc.apache.org/downloads.cgi…
Dentro da pasta LIB tem vários arquivos, é necessário apenas:
commons-logging-1.1.1.jar
httpclient-4.2.1.jar
httpcore-4.2.1.jar
Essa coisa aí em cima, que como diz o Alexandre, eu “penso que é código”, mas que não é, acessa o site do meuip.com.br, e lê a resposta para a requisição, procura no código html a parte onde tem o meu ip, e me devolve ele…
Lembrando que se o meuip.com.br fechar, vai ferrar a paçoca…
Não aconselho usar isso… hehehehe
Abraços…