NetUtil.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @(#)file NetUtil.java
  3. * @(#)author OK
  4. * @(#)version 1.0
  5. * @(#)date 2014.09.04
  6. * @(#)since JDK 1.6.24
  7. *
  8. * Copyright (c) www.udapsoft.co.kr, Inc.
  9. */
  10. package kr.co.udapsoft.common.util;
  11. import java.io.BufferedReader;
  12. import java.io.DataOutputStream;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15. import java.net.URL;
  16. import java.net.URLConnection;
  17. import javax.servlet.http.HttpServletRequest;
  18. import kr.co.hsnc.common.logger.Logger;
  19. public class NetUtil {
  20. /**
  21. * targetUrl에 해당하는 화면을 조회
  22. * @param targetUrl 조회할 화면의 URL(Http://localhost:80/test.html)
  23. * @return
  24. */
  25. public static String getHttpPage(String targetUrl) {
  26. URL url = null;
  27. URLConnection con = null;
  28. DataOutputStream out = null;
  29. InputStream in = null;
  30. InputStreamReader insr = null;
  31. BufferedReader br = null;
  32. StringBuffer buf = new StringBuffer();
  33. String str = "";
  34. try {
  35. url = new URL(targetUrl);
  36. con = url.openConnection();
  37. con.setDoInput(true);
  38. con.setDoOutput(true);
  39. con.setUseCaches(false);
  40. con.setRequestProperty("content-Type", "application/x-www-form-urlencoded;charset=utf-8");//euc-kr
  41. in = con.getInputStream();
  42. insr = new InputStreamReader(in);
  43. br = new BufferedReader(insr);
  44. String temp = null;
  45. while((temp = br.readLine())!=null) {
  46. buf.append(temp);
  47. buf.append("\n");
  48. }
  49. return buf.toString();
  50. }
  51. catch(Exception e) {
  52. e.printStackTrace(System.err);
  53. return null;
  54. }
  55. finally {
  56. try {
  57. if( out != null ) {
  58. out.flush();
  59. out.close();
  60. }
  61. if (in != null) {
  62. in.close();
  63. }
  64. }
  65. catch(Exception e) {
  66. }
  67. }
  68. }
  69. /**
  70. * L4 장비 도입후 보완사항 - 클라이언트 유저의 IP 정보 획득
  71. */
  72. public static String getUserAddr(HttpServletRequest request) {
  73. String xForwardedFor = request.getHeader("x-forwarded-for");
  74. String intelSourceIP = request.getHeader("INTEL_SOURCE_IP");
  75. String RemoteAddr = request.getRemoteAddr();
  76. String return_ip = null;
  77. try {
  78. if ( xForwardedFor == null || xForwardedFor.length() < 4 ) {
  79. if( intelSourceIP == null ) return_ip = RemoteAddr ;
  80. else return_ip = intelSourceIP;
  81. } else {
  82. return_ip = xForwardedFor;
  83. }
  84. }
  85. catch(Exception wafe) {
  86. Logger.err.println("[WAFException]SignOnFilter.getUserAddr() -> " + wafe);
  87. wafe.printStackTrace();
  88. Logger.err.println("");
  89. }
  90. finally {
  91. return return_ip;
  92. }
  93. }
  94. }