오랜만에 자바하니까 귀찮긔...

public String getHTTPDocuement(String urlString, String [] postParameterNames, String [] postParameterValues) {
        String resultDocument = "";

        try {
            URL url = new URL(urlString);
            HttpURLConnection http = null;
           
            HttpURLConnection.setFollowRedirects(false);
           
            // Redirects HTTP documents at most 5 times
            int numRedirects = 0;
            while( true ) {
                // Opens the URL
                http = (HttpURLConnection)url.openConnection();

                // Sets the parameters
                // This logic should be only executed at first time
                if(numRedirects==0 && postParameterNames!=null && postParameterValues!=null) {
                    String postParameterData = "";

                    http.setDoOutput(true);
                    http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    http.setRequestMethod("POST"); // can be skipped

                    for (int j = 0; j < postParameterNames.length; j++) {
                        postParameterData += postParameterNames[j] + "=" + URLEncoder.encode(postParameterValues[j]) + "&";
                    }

                    OutputStream outputStream = http.getOutputStream();

                    outputStream.write( postParameterData.getBytes("UTF-8") );
                    outputStream.flush();
                    outputStream.close();
                }

                // Checks whether the HTTP document want to redirect
                int responseCode = http.getResponseCode();
                if (responseCode >= 300 && responseCode <= 307 && responseCode != 306 && responseCode != HttpURLConnection.HTTP_NOT_MODIFIED ) {
                    URL base = http.getURL();
                    String loc = http.getHeaderField("Location");
                    if (loc != null)
                    {
                        url = new URL(base, loc);
                    }
                    http.disconnect();
                    if (url == null
                            // Allows a redirection from unsecure protocol to secure protocol or vice versa
                            //|| !(target.getProtocol().equals("http")
                            //|| target.getProtocol().equals("https"))
                            || numRedirects >= 5)
                    {
                        throw new SecurityException("illegal URL redirect");
                    }
                    numRedirects++;
                    continue;
                }
               
                // No more redirections
                break;
            }

            // Gets the HTTP response
            BufferedReader inputBufferReader = new BufferedReader(new InputStreamReader(http.getInputStream()));
            StringBuffer buffer = new StringBuffer();

            int c;
            while((c=inputBufferReader.read()) != -1){
                buffer.append((char)c);
            }

            resultDocument = buffer.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultDocument;
    }
Posted by 배트
,