2015년 10월 3일 토요일

[java]HttpClient 와 Swing 컴포넌트를 이용해 ip 팝업 띄우기 소스

import java.io.BufferedReader;
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);

}

}

댓글 없음:

댓글 쓰기

[springboot]실제 JWT 발급 및 검증 구현

실제 JWT 발급 및 검증 구현 이전 단계에서 만든 임시 토큰을 실제 암호화된 JWT(JSON Web Token)로 대체하고, Spring Security 필터를 통해 API 요청을 보호하는 방법을 구현합니다. Part 1: 백엔드 (Spring ...