소스
public static void main(String[] args) {
String clientId = "xxxxx"; // 애플리케이션 클라이언트 아이디값";
String clientSecret = "xxxxx"; // 애플리케이션 클라이언트 시크릿값";
try {
String orgUrl = URLEncoder.encode("https://news.naver.com/main/read.nhn?mode=LS2D&mid=shm&sid1=105&sid2=230&oid=277&aid=0004327179", "utf-8");
String apiURL = "https://openapi.naver.com/v1/util/shorturl";
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("X-Naver-Client-Id", clientId);
con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
// post request
String postParams = "url=" + orgUrl;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // 정상 호출
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else { // 에러 발생
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
br.close();
System.out.println(response.toString());
} catch (Exception e) {
System.out.println(e);
}
}
결과
{"message":"ok","result":{"hash":"G1vF5B18","url":"http://me2.do/G1vF5B18","orgUrl":"https://news.naver.com/main/read.nhn?mode=LS2D&mid=shm&sid1=105&sid2=230&oid=277&aid=0004327179"},"code":"200"}
Java - XML 파싱 후 List로 출력하기 (0) | 2018.10.04 |
---|---|
Java - 네이버 캡챠 API 이미지 발급/수신/비교 (0) | 2018.10.04 |
Java - 네이버 블로그 검색 API (0) | 2018.10.04 |
Java - URL 쿼리를 post 방식으로 전송하기 (0) | 2018.10.02 |
Java - 연결된 url로부터 데이터 읽기 (0) | 2018.10.02 |
댓글 영역