HttpURLConnection 원 격 인터페이스 호출

1333 단어 Java
public static String ResultToString(InputStream inputStream){  
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);  
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
        StringBuilder result = new StringBuilder();  
        String line = null;  
        try {  
            while((line = bufferedReader.readLine()) != null){  
                result.append(line + "
"); } } catch (IOException e) { e.printStackTrace(); } finally { try{ inputStreamReader.close(); inputStream.close(); bufferedReader.close(); }catch(IOException e){ e.printStackTrace(); } } return result.toString(); } public static void main(String[] args) throws IOException { URL url = new URL("http:www.baidu.com") ; HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection() ; urlConnection.setRequestMethod("GET") ; urlConnection.connect() ; InputStream inputStream = urlConnection.getInputStream() ; String result = ConvertToString(inputStream) ; System.out.println(result); }

좋은 웹페이지 즐겨찾기