import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
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 javax.swing.JOptionPane;
@SuppressWarnings("deprecation")
public class WhatIsMyIp {
public static void main(String[] args) {
// HttpClient 생성
HttpClient httpclient = new DefaultHttpClient();
String url = "http://ipconfig.co.kr";
String charSet = "euc-kr";
//String charSet = "UTF-8";
String result = "";
try {
// HttpGet생성
HttpGet httpget = new HttpGet(url);
//System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
//System.out.println("Response content length: "
// + entity.getContentLength());
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(),charSet));
String line = "";
while ((line = rd.readLine()) != null) {
if(line.startsWith("IP address :")){
result = line;
}
}
}
httpget.abort();
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
result = result.split(":")[1];
result = result.trim();
result = result.split(">")[1].split("<")[0];
//sSystem.out.println(result);
JOptionPane.showMessageDialog(null, result);
}
}
댓글 없음:
댓글 쓰기